title: Add vline to geom_density and shade confidence interval of mean R
body: <p>After reading through different posts, I found out how to add a vline of mean to density plots as shown <a href="http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/" rel="nofollow noreferrer">here</a>. 
  Using the data provided in the above link:</p>
  
  <p>1) How can one add 95% confidence intervals around the mean using geom_ribbon? 
  CIs can be computed as </p>
  
  <pre><code>#computation of the standard error of the mean
  sem&lt;-sd(x)/sqrt(length(x))
  #95% confidence intervals of the mean
  c(mean(x)-2*sem,mean(x)+2*sem)
  </code></pre>
  
  <p>2) How can one limit the vline to the region under the curve? You will see in the picture below that vline plots outside the curve.</p>
  
  <p>Sample data very close to my real problem can be found at <a href="https://www.dropbox.com/s/bvvfdpgekbjyjh0/test.csv?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/bvvfdpgekbjyjh0/test.csv?dl=0</a></p>
  
  <p><a href="https://i.stack.imgur.com/1nlY3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1nlY3.png" alt="sample plot"></a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Using real data in the link above, I have tried the following using @beetroot's answer.</p>
  
  <pre><code># Find the mean of each group
  dat=me
  library(dplyr)
  library(plyr)
  cdat &lt;- ddply(data,.(direction,cond), summarise, rating.mean=mean(rating,na.rm=T))# summarize by season and variable
  cdat
  
  #ggplot
  p=ggplot(data,aes(x = rating)) + 
    geom_density(aes(colour = cond),size=1.3,adjust=4)+
    facet_grid(.~direction, scales="free")+
    xlab(NULL) + ylab("Density")
  p=p+coord_cartesian(xlim = c(0, 130))+scale_color_manual(name="",values=c("blue","#00BA38","#F8766D"))+
    scale_fill_manual(values=c("blue", "#00BA38", "#F8766D"))+
    theme(legend.title = element_text(colour="black", size=15, face="plain"))+
    theme(legend.text = element_text(colour="black", size = 15, face = "plain"))+
    theme(title = red.bold.italic.text, axis.title = red.bold.italic.text)+
    theme(strip.text.x = element_text(size=20, color="black",face="plain"))+ # facet labels
    ggtitle("SAMPLE A") +theme(plot.title = element_text(size = 20, face = "bold"))+
      theme(axis.text = blue.bold.italic.16.text)+ theme(legend.position = "none")+
    geom_vline(data=cdat, aes(xintercept=rating.mean, color=cond),linetype="dotted",size=1)
  p
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/3Qj6u.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3Qj6u.png" alt="sample plot from data"></a></p>
  
  <pre><code>## implementing @beetroot's code to restrict lines under the curve and shade CIs around the mean
  # I will use ddply for mean and CIs
  cdat &lt;- ddply(data,.(direction,cond), summarise, rating.mean=mean(rating,na.rm=T),
                sem = sd(rating,na.rm=T)/sqrt(length(rating)),
                ci.low = mean(rating,na.rm=T) - 2*sem,
                ci.upp = mean(rating,na.rm=T) + 2*sem)# summarize by direction and variable
  
  
  #In order to limit the lines to the outline of the curves you first need to find out which y values
  #of the curves correspond to the means, e.g. by accessing the density values with ggplot_build and 
  #using approx:
  
     cdat.dens &lt;- ggplot_build(ggplot(data, aes(x=rating, colour=cond)) +
                                facet_grid(.~direction, scales="free")+
                                geom_density(aes(colour = cond),size=1.3,adjust=4))$data[[1]] %&gt;%
    mutate(cond = ifelse(group==1, "A",
                         ifelse(group==2, "B","C"))) %&gt;%
    left_join(cdat) %&gt;%
    select(y, x, cond, rating.mean, sem, ci.low, ci.upp) %&gt;%
    group_by(cond) %&gt;%
    mutate(dens.mean = approx(x, y, xout = rating.mean)[[2]],
           dens.cilow = approx(x, y, xout = ci.low)[[2]],
           dens.ciupp = approx(x, y, xout = ci.upp)[[2]]) %&gt;%
    select(-y, -x) %&gt;%
    slice(1)
  
   cdat.dens
  
  #---
   #You can then combine everything with various geom_segments:
  
     ggplot(data, aes(x=rating, colour=cond)) +
     geom_density(data = data, aes(x = rating, colour = cond),size=1.3,adjust=4) +facet_grid(.~direction, scales="free")+
     geom_segment(data = cdat.dens, aes(x = rating.mean, xend = rating.mean, y = 0, yend = dens.mean, colour = cond),
                  linetype = "dashed", size = 1) +
     geom_segment(data = cdat.dens, aes(x = ci.low, xend = ci.low, y = 0, yend = dens.cilow, colour = cond),
                  linetype = "dotted", size = 1) +
     geom_segment(data = cdat.dens, aes(x = ci.upp, xend = ci.upp, y = 0, yend = dens.ciupp, colour = cond),
                  linetype = "dotted", size = 1)
  </code></pre>
  
  <p>Gives this:</p>
  
  <p><a href="https://i.stack.imgur.com/dscCo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dscCo.png" alt="enter image description here"></a></p>
  
  <p>You will notice the mean and CIs are not aligned as in the original plot. What am I not doing right @beetroot?</p>