title: How do I restrict the standard deviation bars in my barplot to a maximum value?
body: <p>I am creating barplots with standard deviation bars using <code>ggplot2</code>.  My data frame is quite large but here is a truncated version for example:</p>
  
  <pre><code>SampleName  Target.ID   Maj.Allele.Freq SD  AVG.MAF
  W15-P2-1    rs1005533   99.74811083 24.98883743 93.70753223
  W15-P2-2    rs1005533   100 24.98883743 93.70753223
  W15-P2-3    rs1005533   100 24.98883743 93.70753223
  W15-P2-4    rs1005533   100 24.98883743 93.70753223
  W15-P2-1    rs1005533   99.94819995 24.98883743 93.70753223
  W15-P2-2    rs1005533   100 24.98883743 93.70753223
  W15-P2-3    rs1005533   100 24.98883743 93.70753223
  W15-P2-4    rs1005533   100 24.98883743 93.70753223
  W21-P2-1    rs1005533   100 24.98883743 93.70753223
  W21-P2-2    rs1005533   100 24.98883743 93.70753223
  W21-P2-3    rs1005533   99.90044798 24.98883743 93.70753223
  W21-P2-4    rs1005533   99.72375691 24.98883743 93.70753223
  W21-P2-1    rs1005533   100 24.98883743 93.70753223
  W21-P2-2    rs1005533   100 24.98883743 93.70753223
  W21-P2-3    rs1005533   100 24.98883743 93.70753223
  W21-P2-4    rs1005533   0   24.98883743 93.70753223
  W15-P2-1    rs10092491  52.40641711 1.340954343 51.8604281
  W15-P2-2    rs10092491  53.69923603 1.340954343 51.8604281
  W15-P2-3    rs10092491  52.56689284 1.340954343 51.8604281
  W15-P2-4    rs10092491  50.11764706 1.340954343 51.8604281
  W15-P2-1    rs10092491  50.30094583 1.340954343 51.8604281
  W15-P2-2    rs10092491  50.96277279 1.340954343 51.8604281
  W15-P2-3    rs10092491  50.94102886 1.340954343 51.8604281
  W15-P2-4    rs10092491  51.2849162  1.340954343 51.8604281
  W21-P2-1    rs10092491  53.56976202 1.340954343 51.8604281
  W21-P2-2    rs10092491  50.27861123 1.340954343 51.8604281
  W21-P2-3    rs10092491  52.8358209  1.340954343 51.8604281
  W21-P2-4    rs10092491  51.42585551 1.340954343 51.8604281
  W21-P2-1    rs10092491  52.77890467 1.340954343 51.8604281
  W21-P2-2    rs10092491  52.89017341 1.340954343 51.8604281
  W21-P2-3    rs10092491  53.70786517 1.340954343 51.8604281
  W21-P2-4    rs10092491  50  1.340954343 51.8604281
  </code></pre>
  
  <p>Because the average values in the last column (<code>AVG.MAF</code>) can produce standard deviation bars that exceed the maximum of 100, the plot shows the bars beyond the limit on the y axis of 100.  </p>
  
  <p><img src="https://i.stack.imgur.com/dOMso.png" alt="Example Standard Deviation bars extend beyond 100."></p>
  
  <p>Here is the code to create the above plot:</p>
  
  <pre><code>pe1 = ggplot(half1, aes(x=Target.ID, y=AVG.MAF))+
   geom_bar(stat = "identity", position = "dodge", colour = "black", 
   width = 0.5, fill = "yellowgreen")+xlab("")+
   ylab("Average Major Allele Frequency")+
   labs(title="Allele Balance AmpliSeq Identity Sample P2")+
   geom_errorbar(aes(ymin = AVG.MAF-SD, ymax = AVG.MAF+SD), 
   width = 0.4, position = position_dodge(0.9), 
     size = 0.6)+
   theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5))
  </code></pre>
  
  <p>I tried truncating the plot using <code>coord_cartesian</code> but this kind of makes the plot look like I am hiding some data:</p>
  
  <p><img src="https://i.stack.imgur.com/ysAUm.png" alt="Here the top of the standard deviation bars is cut off"></p>
  
  <p>Here is the code to create the plot with the standard deviation bars cut off:</p>
  
  <pre><code>pe1 = ggplot(half1, aes(x=Target.ID, y=AVG.MAF))+geom_bar(stat = "identity", position = "dodge", colour = "black", width = 0.5, fill = "yellowgreen")+xlab("")+ylab("Average Major Allele Frequency")+labs(title="Allele Balance AmpliSeq Identity Sample P2")+geom_errorbar(aes(ymin = AVG.MAF-SD, ymax = AVG.MAF+SD), width = 0.4, position = position_dodge(0.9), size = 0.6)+theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = .5))+coord_cartesian(ylim=c(0,100))
  </code></pre>
  
  <p>It seems like there has to be a way to restrict the standard deviation bars to my intended ymax of 100 and still keep the top horizontal bar visible in the plot.  Does any one know how to do this?</p>