title: Set axis tick and label to include maximum value on bar plot
body: <p>I am using base R to create multiple side-by-side bar plots (I prefer to avoid using <code>ggplot</code> because I am often limited in functionality). I find the result not visually pleasing because the axis ticks don't line up at all : sometimes the top of the axis is well below the maximum value and sometimes it is above. Is there a way to set it so that the final axis tick value includes the maximum value? It can't align perfectly because each graph has its own range and thus its own set of ticks values but I would like to at least have it so that the style is consistent across 12 graphs.</p>
  <p>I am creating the series using a function (and a loop) so I would prefer an automated solution (rather than adjusting each graph with setting the maximum limit <code>axis()</code> separately)</p>
  <p>Here is a simplified sample with the <code>iris</code> dataset. The issue that comes up is that the axis ends at 6 in the first panel, which is below the maximum value including error bars (7.2238), whereas in the second panel the axis ends above the maximum value.</p>
  <pre><code>library(vegan)
  data(iris)
  
  x1_mean&lt;-tapply(iris$Sepal.Length, iris$Species, FUN=mean)
  x1_sd&lt;-tapply(iris$Sepal.Length, iris$Species, FUN=sd)
  
  
  x2_mean&lt;-tapply(iris$Petal.Width, iris$Species, FUN=mean)
  x2_sd&lt;-tapply(iris$Petal.Width, iris$Species, FUN=sd)
  
  par(mfrow=c(1,2))
  br1=barplot(x1_mean, ylim=c(0, (max(x1_mean)+max(x1_sd))*1.1))
  errbar(x = br1, y = x1_mean, 
                       yplus = x1_mean+x1_sd, 
                       yminus = x1_mean-x1_sd, add = T, cex = 0)
  
  
  br2=barplot(x2_mean, ylim=c(0, (max(x2_mean)+max(x2_sd))*1.1))
  errbar(x = br2, y = x2_mean, 
         yplus = x2_mean+x2_sd, 
         yminus = x2_mean-x2_sd, add = T, cex = 0)
  </code></pre>
  <p><a href="https://i.stack.imgur.com/8upL6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8upL6.jpg" alt="enter image description here" /></a></p>
  <p>EDIT/PROGRESS:</p>
  <p>I have managed to extract the maximum value of the axis using <code>par(&quot;yaxp&quot;)</code> and used that to add an extra tick so that the last tick value is greater than the maximum value on the graph. But, it forces me to actually plot the default graph, which creates two graphs.
  I've also included a simplified version of the function (using <code>iris</code> as a sample dataset) that I am trying to build, which will be perhaps clearer about my aims.</p>
  <pre><code>library(vegan)
  data(iris)
  
  barplot_adjust&lt;- function(data, metric,...) {
    x_means&lt;-tapply(data[,metric], list(data$Species), FUN=mean)
    x_sd&lt;-tapply(data[,metric], list(data$Species), FUN=sd)
    br1 &lt;- barplot(height = x_means, names.arg = names(x_sd), ylim = c(0, (max(x_means+x_sd))*1.1), 
                   main=metric, las=0,plot=TRUE,
                   ...)
     if( par(&quot;yaxp&quot;)[2]&lt;(max(x_means+x_sd))*1.1 )
    {ymx=par(&quot;yaxp&quot;)[2]+par(&quot;yaxp&quot;)[2]/par(&quot;yaxp&quot;)[3]}else{ymx=par(&quot;yaxp&quot;)[2]}
    br1 &lt;- barplot(height = x_means, names.arg = names(x_sd), ylim = c(0, ymx), 
                   main=metric, las=0,plot=TRUE,
                   ...)
    print(ymx)
    errbar(x = br1, y = x_means, yplus = x_means+x_sd, yminus = x_means-x_sd, add = T, cex = 0)
  }
  
  par(mfrow=c(2,2))
  barplot_adjust(data=iris, metric=&quot;Sepal.Length&quot;)
  barplot_adjust(data=iris, metric=&quot;Petal.Width&quot;)
  </code></pre>