title: ggplot2, histogram: why do y = ..density.. and stat = "density" differ?
body: <p>Say I have this data frame <code>df</code>:</p>
  
  <pre><code>structure(list(max.diff = c(6.02, 7.56, 7.79, 7.43, 7.21, 7.65, 
  8.1, 7.35, 7.57, 9.09, 6.21, 8.2, 6.82, 7.18, 7.78, 8.27, 6.85, 
  6.72, 6.67, 6.99, 7.32, 6.59, 6.86, 6.02, 8.5, 7.25, 5.18, 8.85, 
  5.44, 6.44, 7.85, 6.25, 9.06, 8.19, 5.08, 6.26, 8.92, 6.83, 6.5, 
  7.55, 7.31, 5.83, 5.55, 4.29, 8.29, 8.72, 9.5)), class = "data.frame", row.names = c(NA, 
  -47L), .Names = "max.diff")
  </code></pre>
  
  <p>I want to plot this as a density plot using <code>ggplot2</code>:</p>
  
  <pre><code>p &lt;- ggplot(df, aes(x = max.diff)) 
  p &lt;- p + geom_histogram(stat = "density")
  print(p)
  </code></pre>
  
  <p>which gives,</p>
  
  <p><a href="https://i.stack.imgur.com/DL2zB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DL2zB.png" alt="enter image description here"></a></p>
  
  <p>Now, a naive question: why doesn't this give the same result?</p>
  
  <pre><code>p &lt;- ggplot(df, aes(x = max.diff)) 
  p &lt;- p + geom_histogram(aes(y = ..density..))
  print(p)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/T97iF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T97iF.png" alt="enter image description here"></a></p>
  
  <p>Is this because of the chosen <code>binwidth</code> or number of <code>bins</code> or some other parameter? So far, I haven't been able to tweak those parameters to make them the same. Or am I plotting something quite different?</p>