Search code examples
rggplot2plotlinegrapherrorbar

How to make half-wiskers in a ggplot2 line graph?


I make very slow progress in R but now I'm able to do some stuff.
Right now I'm plotting the effects of 4 treatments on plant growth in one graph. As you can see the errorbars overlap which is why I made them different colors. I think in order to make the graph clearer it's better to use the lower errorbars as "half wiskers" for the lower 2 lines, and the upper errorbars for the top two lines (like I have now), see the attached image for reference
see attached image

Is that doable with the way my script is set up now?
Here is part of my script of the plot, I have a lot more but this is where I specify the plot itself my plot (leaving out the aesthetics and stuff), thanks in advance:

"soda1" is my altered dataframe, setup in a clear way, "sdtv" are my standard deviations for each timepoint/treatment, "oppervlak" is my y variable and "Measuring Date" is my x variable. "Tray ID" is the treatment, so my grouping variable.

p <- ggplot(soda1, aes(x=reorder(`Measuring Date`, oppervlak), y=`oppervlak`, group=`Tray ID`, fill=`Tray ID`, colour = `Tray ID` )) + 
 scale_fill_brewer(palette = "Spectral") + 
 geom_errorbar(data=soda1, mapping=aes(ymin=oppervlak, ymax=oppervlak+sdtv, group=`Tray ID`), width=0.1) + 
 geom_line(aes(linetype=`Tray ID`)) + 
 geom_point(mapping=aes(x=`Measuring Date`, y=oppervlak, shape=`Tray ID`)) 

print(p)

Solution

  • Showing only one side of errorbars can hide an overlap in the uncertainty between the distribution of two or more variables or measurements. Instead of hiding this overlap, you could adjust the position of your errorbars horizontally very easily by adding position=position_dodge(width=) to your call to geom_errorbar().

    For example:

    library(ggplot2)
    
    # some random data with two factors
    df <- data.frame(a=rep(1:10, times=2),
                     b=runif(20),
                     treat=as.factor(rep(c(0,1), each=10)),
                     errormax=runif(20),
                     errormin=runif(20))
    
    # plotting both sides of the errorbars, but dodging them horizontally
    p <- ggplot(data=df, aes(x=a, y=b, colour=treat)) +
      geom_line() +
      geom_errorbar(data=df, aes(ymin=b-errormin, ymax=b+errormax),
                    position=position_dodge(width=0.25))
    

    error_dodge