title: Overlay raw data onto geom_bar
body: <p>I have a data-frame arranged as follows:</p>
  
  <pre><code>condition,treatment,value
  A        ,  one    , 2
  A        ,  one    , 1
  A        ,  two    , 4
  A        ,  two    , 2
  ...
  D        ,  two    , 3
  </code></pre>
  
  <p>I have used ggplot2 to make a grouped bar plot that looks like this:
  <img src="https://i.stack.imgur.com/HG3Mc.png" alt="example plot"></p>
  
  <p>The bars are grouped by "condition" and the colours indicate "treatment." The bar heights are the mean of the values for each condition/treatment pair. I achieved this by creating a new data frame containing the mean and standard error (for the error bars) for all the points that will make up each group. </p>
  
  <p>What I would like to do is superimpose the raw <em>jittered</em> data to produce a bar-chart version of this box plot: <a href="http://docs.ggplot2.org/0.9.3.1/geom_boxplot-6.png" rel="noreferrer">http://docs.ggplot2.org/0.9.3.1/geom_boxplot-6.png</a> [I realise that a box plot would probably be better, but my hands are tied because the client is pathologically attached to bar charts]</p>
  
  <p>I have tried adding a geom_point object to my plot and feeding it the raw data (rather than the aggregated means which were used to make the bars). This sort of works, but it plots the raw values at the wrong x axis locations. They appear at the points at which the red and grey bars join, rather than at the centres of the appropriate bar. So my plot looks like this:</p>
  
  <p><img src="https://i.stack.imgur.com/i7eHD.png" alt="wrong-looking plot"></p>
  
  <p>I can not figure out how to shift the points by a fixed amount and then jitter them in order to get them centered over the correct bar. Anyone know? Is there, perhaps, a better way of achieving what I'm trying to do? </p>
  
  <p>What follows is a minimal example that shows the problem I have:</p>
  
  <pre><code>#Make some fake data
  ex=data.frame(cond=rep(c('a','b','c','d'),each=8),
      treat=rep(rep(c('one','two'),4),each=4),
      value=rnorm(32) + rep(c(3,1,4,2),each=4) )
  
  #Calculate the mean and SD of each condition/treatment pair
  agg=aggregate(value~cond*treat, data=ex, FUN="mean") #mean
  agg$sd=aggregate(value~cond*treat, data=ex, FUN="sd")$value #add the SD 
  
  
  dodge &lt;- position_dodge(width=0.9) 
  limits &lt;- aes(ymax=value+sd, ymin=value-sd) #Set up the error bars
  
  p &lt;- ggplot(agg, aes(fill=treat, y=value, x=cond)) 
  
  #Plot, attempting to overlay the raw data
  print(
         p + geom_bar(position=dodge, stat="identity") +
         geom_errorbar(limits, position=dodge, width=0.25) + 
         geom_point(data= ex[ex$treat=='one',], colour="green", size=3) +
         geom_point(data= ex[ex$treat=='two',], colour="pink", size=3)
  )
  </code></pre>