title: How to get a "perfect " Y-axis for hist(nclass=nclass.scott)?
body: <p>Occasionally <code>hist(..., nclass=nclass.scott)</code> produces a histogram where the maximum bar extends over the top of the y axis. You may try this example a few times:</p>
  
  <pre><code>x &lt;- sample(1000000, 500, replace=TRUE)
  h &lt;- hist(x,nclass=nclass.scott)
  text(x=h$mids, y=h$counts, labels=h$counts, pos=3, col="red")
  </code></pre>
  
  <p>Example:<a href="https://i.stack.imgur.com/zSsCX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zSsCX.png" alt="enter image description here"></a></p>
  
  <p>Occasionally the red number over the highest bar cannot be presented as it seems to be clipped by the plot region. I could add <code>ylim=...</code>, but it's quite tricky to get the maximum height of the bar.</p>
  
  <p>Even when knowing the maximum height, <code>ylim=(0, max)</code> has the problem that <code>max</code> may be ignored: For example, when maximum is 527, then the upper displayed y-axis label is 500, even if <code>ylim=(0, 527)</code> is specified. When using <code>600</code> instead, it works, but then the y-axis is a bit too long...</p>
  
  <p>If that is not a bug of R (3.3.3), what is an elegant (minimalistic) solution?</p>