title: errorbar in percent stacked barchart ggplot2
body: <p>I have these data:</p>
  <pre><code>db&lt;- data.frame(id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48),
  Time = c(1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2),
  Part  =c(Iv, Iv, Iv, Iv, Id, Id, Id, Id, Iv, Iv, Iv, Iv, Id, Id, Id, Id, Iv, Iv, Iv, Iv, Id, Id, Id, Id, Iv, Iv, Iv, Iv, Id, Id, Id, Id, Iv, Iv, Iv, Iv, Id, Id, Id, Id, Iv, Iv, Iv, Iv, Id, Id, Id, Id), 
  
  Cicle = c(I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, I, II, II, II, II, II, II, II, II, II, II, II, II, II, II, II, II, III, III, III, III, III, III, III, III, III, III, III, III, III, III, III, III), 
  
  val = c(24.5, 35.5, 31.5, 25, 25.5, 13.5, 18, 24.5, 27.5, 29, 19, 33, 22.5, 20, 28.5, 19.5, 21, 28.5, 22.5, 22.5, 28.5, 21, 26.5, 26.5, 20.5, 26.5, 22, 30, 28.5, 23, 27.5, 20, 17.5, 29.5, 27, 22, 32, 21, 22.5, 27.5, 26, 23.5, 18.5, 27.5, 23, 23, 31.5, 22.5))
  </code></pre>
  <p>I would like to obtain this percent stacked barchart with errorbars:
  <a href="https://i.stack.imgur.com/jm9G2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jm9G2.png" alt="enter image description here" /></a></p>
  <p>I've tried in this way:</p>
  <pre><code>db$Cicle &lt;- factor (db$Cicle, levels=c(&quot;I&quot;, &quot;II&quot;, &quot;III&quot;))
  
  db &lt;- db %&gt;% 
    group_by(Time, Part, Cicle) %&gt;% 
    summarise(val = sum(val))
  
  sd&lt;-sd(db$val)
  
  b&lt;-ggplot(db, aes(x = Time, y = val, fill= Part)) + 
    geom_bar(position=&quot;fill&quot;, stat=&quot;identity&quot;, colour=&quot;black&quot;)+ facet_grid(. ~ Cicle)+
    scale_fill_manual(values = c(&quot;#DADAEB&quot;, &quot;#9E9AC8&quot;))
  b + labs(x=&quot;Time&quot;, y=&quot;%&quot;, fill=&quot;&quot;) + theme_bw() + 
    theme(panel.border = element_blank(), panel.grid.major = element_blank(),panel.grid.minor = element_blank(), axis.line = element_line(colour =&quot;black&quot;)) + 
    theme(axis.text=element_text(size=14,face=&quot;bold&quot;),axis.title=element_text(size=14,face=&quot;bold&quot;)) + 
    theme(legend.title = element_text(size=14),legend.text = element_text(size=12)) +  
    theme(strip.text = element_text(size = 14, face=&quot;bold&quot;))  +
    theme(plot.title = element_text(size = 20, face = &quot;bold&quot;, hjust = 0.5))
  
  
  
  
  ggplot(db, aes(x = Time, y = val, fill= Part)) + 
    geom_bar(position=&quot;fill&quot;, stat=&quot;identity&quot;, colour=&quot;black&quot;)+ facet_grid(. ~ Cicle)+
    scale_fill_manual(values = c(&quot;#DADAEB&quot;, &quot;#9E9AC8&quot;)) +
    geom_errorbar(aes(ymin=val-sd, ymax=val+sd), width=.2,
                  position=position_dodge(.9)) 
  
  
  </code></pre>
  <p>But I've obtained this plot, just perfect but withour errorbars:</p>
  <p><a href="https://i.stack.imgur.com/jMUX8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jMUX8.png" alt="enter image description here" /></a></p>
  <p>and then this plot with errorbars but wrong:</p>
  <p><a href="https://i.stack.imgur.com/uXXpH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uXXpH.png" alt="enter image description here" /></a></p>
  <p>How can I fix it?</p>
  <p>Thank you!</p>
  <p>As suggested by @harre I've create a new variable:</p>
  <pre><code>db$val1&lt;-(db$val)/100
  </code></pre>
  <p>then I've tried again the code but errorbars random placed..</p>
  <pre><code>ggplot(db, aes(x = Time, y = val1, fill= Part)) + 
    geom_bar(position=&quot;fill&quot;, stat=&quot;identity&quot;, colour=&quot;black&quot;)+ facet_grid(. ~ Cicle)+
    scale_fill_manual(values = c(&quot;#DADAEB&quot;, &quot;#9E9AC8&quot;)) +
    geom_errorbar(aes(ymin=val1-sd, ymax=val1+sd), width=.2,position=position_dodge(.9)) 
  </code></pre>
  <p>Oh well..
  Why this?</p>
  <p><a href="https://i.stack.imgur.com/JkxPj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JkxPj.png" alt="enter image description here" /></a></p>