title: How to manually add an error bar (95% CI) to a barplot
body: <p>I drew a cumulative barplot with ggplot2:</p>
  
  <pre><code>a &lt;- ggplot(datasilice2) +
    aes(x = Duree_exposition_4gpes, fill = factor(Antinucléaires_sup_ou_egal_200)) +
    geom_bar(position = "fill", show.legend = F, colour = "NA", alpha = 0.4) +
    scale_fill_manual(values=c(NA, "red")) +
    scale_y_continuous(name = "Yes", limits=c(0,0.5),oob = rescale_none, breaks = seq(0,0.5,0.05), labels = percent_format(suffix = "%", accuracy = 1))
  </code></pre>
  
  <p>It looks like this:
  <img src="https://i.stack.imgur.com/I0ULz.png" alt="barplot without error bar"></p>
  
  <p>I want to add an error bar for each column.</p>
  
  <p>So first, I manually calculated each 95% CI :</p>
  
  <pre><code>binom.test(2,45)$conf.int*100 
  binom.test(3,10)$conf.int*100 
  binom.test(5,18)$conf.int*100 
  binom.test(12,27)$conf.int*100 
  </code></pre>
  
  <p>Then, I use the geom_errorbar function:</p>
  
  <pre><code>a + geom_errorbar(ymin=0.2, ymax=0.4,width = 0.2, size=1)
  </code></pre>
  
  <p>It does draw an error bar, but every error bars is the same:
  <img src="https://i.stack.imgur.com/6HgPm.png" alt="same error bar"></p>
  
  <p>Right now I am stuck: I was not able to find the solution for setting every error bar manually. I imagined something like this, but it won't work:</p>
  
  <pre><code>a + geom_errorbar(ymin=c(0.2,0.22,0.23,0.30), ymax=c(0.4,0.43,0.35,0.50))
  #random number, I will put CI here
  </code></pre>
  
  <p>Here is the error I get:</p>
  
  <pre><code>Aesthetics must be either length 1 or the same as the data (100): ymin, ymax
  </code></pre>
  
  <p>Any ideas of what to do to get something like this? <img src="https://i.stack.imgur.com/DMvu9.png" alt="(done with paint)">
  Thanks!</p>
  
  <p>EDIT:
  Here is the 10 first rows of my database (datasilice2):</p>
  
  <pre><code>&gt; dput(head(datasilice2, 10))
  structure(list(Duree_exposition_4gpes = c(3, 3, 0, 1, 0, 1, 2, 
  2, 1, 3), Antinucléaires_sup_ou_egal_200 = c(1, 0, 0, 1, 1, 1, 
  0, 0, 1, 1)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
  "data.frame"))
  </code></pre>
  
  <p>As suggested, I created a new dataframe and tried to feed that into the geom_errorbar:</p>
  
  <pre><code>a &lt;- ggplot(datasilice2) +
    aes(x = Duree_exposition_4gpes, fill = factor(Antinucléaires_sup_ou_egal_200)) +
    geom_bar(position = "fill", show.legend = F, colour = "NA", alpha = 0.4) +
    scale_fill_manual(values=c(NA, "red")) +
    scale_y_continuous(name = "Yes", limits=c(0,0.5),oob = rescale_none, breaks = seq(0,0.5,0.05), labels = percent_format(suffix = "%", accuracy = 1))
  
  newdf &lt;- data.frame(xcol=c(0,1,2,3),ymincol=c(0.2,0.3,0.4,0.5),ymaxcol=c(0.25,0.30,0.45,0.5))
  a + geom_errorbar(data=newdf,aes(x=xcol,ymin=ymincol,ymax=ymaxcol))
  </code></pre>
  
  <p>But here is a new error:</p>
  
  <pre><code>Error in factor(Antinucléaires_sup_ou_egal_200) : 
    object 'Antinucléaires_sup_ou_egal_200' not found
  </code></pre>