Search code examples
rggplot2linequantilehorizontal-line

Plot Percentile Indication in R / GGPLOT2


I have a basic plot of a two column data frame (x = "Periods" and y = "Range").

enter image description here

library (ggplot2)
qplot (Periods, Range, data=twocoltest, color=Periods, size = 3,) + geom_jitter(position=position_jitter(width=0.2))

I am trying to add a horizontal line at each period below which lie 90% of all the observations for that period. (It doesn't have to be a horizontal line, any visual indication per period would suffice).

Any help would be greatly appreciated.


Solution

  • Alrighty, I've read the ggplot help, and here's a go:

    # example data 
    twocoltest <- data.frame(Periods=rep(1:3,each=3),Range=1:9)
    
    library(ggplot2)
    c <- qplot (Periods, Range, data=twocoltest, color=Periods, size = 3,) + geom_jitter(position=position_jitter(width=0.2))
    q90 <- function(x) {quantile(x,probs=0.9)}
    
    c + stat_summary(fun.y=q90, colour="red", geom="crossbar", size = 1, ymin=0, ymax=0) 
    

    enter image description here