title: Error when adding errorbars to ggplot
body: <p>Dear Stackoverflow users,</p>
  <p>I would like to draw a grouped barplot with three independent variables with error bars. I based my graph on an example on Stacked Overflow (stacked bars within grouped bars), using ggplot with geom_bar. When I add the geom_errorbar according to examples of the help pages, I get the following error:
  <code>Error in if (empty(data)) { : missing value where TRUE/FALSE needed</code></p>
  <p>This is the script I use:</p>
  <pre><code>treatment&lt;-rep(c(rep(c(1),8),rep(c(2),8)),2)
  origin&lt;-rep(c(&quot;A&quot;,&quot;B&quot;),16)
  time&lt;-c(rep(c(5),16),rep(c(10),16))
  sulfide&lt;-c(0,10,5,8,9,6,16,18,20,25,50,46,17,58,39,43,20,25,50,46,17,58,39,43,100,120,103,104,150,160,200,180)
  
  Reed&lt;-data.frame(treatment,origin,time,sulfide)
  
  # specify factor types
  Reed$treatment&lt;-as.factor(Reed$treatment)
  Reed$origin&lt;-as.character(Reed$origin)
  Reed$time&lt;-as.factor(Reed$time)
  
  library(ggplot2)
  library(scales)
  
  #draw plot
  ggplot() +geom_bar(data=Reed, aes(y = sulfide, x = treatment, fill=origin), stat=&quot;identity&quot;,position=&quot;dodge&quot;) +theme_bw() + facet_grid( ~ time)+xlab(&quot;treatment&quot;) +ylab(&quot;Sulfide&quot;)+ggtitle(&quot;Time)&quot;)
  </code></pre>
  <p>This is how I added error bars:</p>
  <pre><code>ErrorBars &lt;- function(x, y, upper, lower=upper, length=0.03,...{if(length(x) != length(y) | length(y) !=length(lower) | length(lower) != length(upper))stop(&quot;vectors must be same length&quot;)arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, ...)}#function for errorbars
  
  SE&lt;- function(x) sqrt(var(x,na.rm=TRUE)/length(na.omit(x))) #function for SE
  
  Reed$trt&lt;- paste(Reed$treatment,Reed$origin,sep=&quot;&quot;)#combine treatment and origin to a column 
  mean_Reed&lt;-data.frame(tapply(Reed$sulfide,list(Reed$trt,Reed$time),mean,na.rm=TRUE)) #mean
  SE_Reed&lt;-data.frame(tapply(Reed$sulfide,list(Reed$trt, Reed$time),SE)) # SE 
  
  limits &lt;- aes(ymax = mean_Reed + SE_Reed, ymin=mean_Reed - SE_Reed)# Define the top and bottom of the errorbars
  
  #plot with error bars:
  ggplot() +geom_bar(data=Reed, aes(y = sulfide, x = treatment, fill=origin), stat=&quot;identity&quot;,position=&quot;dodge&quot;) +theme_bw() + facet_grid( ~ time)+xlab(&quot;treatment&quot;) +ylab(&quot;Sulfide&quot;)+ggtitle(&quot;Time)&quot;+ geom_errorbar(limits, width=.2,position=&quot;dodge&quot;) 
  </code></pre>
  <p>I really can't find what I'm doing wrong.
  I hope you can help me:)</p>