Mulligan
Mulligan

Reputation: 95

How to get ggplot_line to stop filling in the space between lines

I'm trying to graph several lines of data (in black) and their averages (in color). For whatever reason, geom_line is shading in the area between the full data lines (but not the averages) - it does the same thing with geom_path but not geom_point. I'm hoping this is just a little typo somewhere, because I've never seen this happen.

Here's what the graph looks like now:

Here's my code:

Plot <- ggplot() + 
    geom_line(data = meltedDF_lines, aes(x = Wavelength, y = Absorbance), color = "black", 
          size = 0.1) + 
    geom_line(data = melted_treatment, inherit.aes = FALSE, 
          aes(x = Wavelength, y = Absorbance, col = Treatment, group = Treatment), size = 
          0.5, na.rm = TRUE) + 
    xlab("Wavelength (" ~ cm^-1 ~ ")") + 
    ylab("Absorbance") + 
    theme(legend.justification = c("right", "top"), 
          axis.text.x = element_text(angle = 20, vjust = 1.2, hjust=1)) + 
    ggtitle("Troubleshooting")
Plot

And here's the beginning to the line data:

head(meltedDF_lines, n = 10)
   Letter Site    Sample Method Treatment Wavelength Absorbance
1       B   C1  F117C503    CHM    CHM.01        402 0.41624870
2       B   C1  F117C503    CHM    CHM.02        402 0.14336000
3       B   C1 F121C1000    CHM    CHM.01        402 0.41037100
4       B   C1 F121C1000    CHM    CHM.02        402 0.28672010
5       B   C1 F121C1000    CHM    CHM.06        402 0.05722035
6       B   C1 F121C1000  CHM_H  CHM.01_H        402 0.10673290
7       B   C1 F121C1000  CHM_H  CHM.02_H        402 0.11100250
8       B   C1 F121C1000  CHM_H  CHM.03_H        402 0.11069650
9       B   C2   F96C843    CHM    CHM.01        402 0.37900400
10      B   C2   F96C843    CHM    CHM.03        402 0.42816850

How do I fix this? I've double checked that everything is numeric and in a dataframe, there are no duplicates or anything in the data - I'm stumped. Thanks!

Upvotes: 1

Views: 1337

Answers (1)

Niels Holst
Niels Holst

Reputation: 626

You have several different y-values for each x-value. geom_line draws a line through all (x,y)-values. Hence you get the vertical lines on each x-value. You could use geom_smooth instead, or reduce the y-values for each value to the average (maybe with standard errorbars).

Upvotes: 2

Related Questions