title: Display the total number of bin elements in a stacked histogram with ggplot2
body: <p>I'd like to show data values on stacked bar chart in ggplot2. After many attempts, the only way I found to show the total amount (for each bean) is using the following code</p>
  
  <pre><code>set.seed(1234)
  
  df &lt;- data.frame(
    sex=factor(rep(c("F", "M"), each=200)),
    weight=round(c(rnorm(200, mean=55, sd=5), rnorm(200, mean=65, sd=5)))
  )
  
  p&lt;-ggplot(df, aes(x=weight, fill=sex, color=sex))
  p&lt;-p + geom_histogram(position="stack", alpha=0.5, binwidth=5)
  
  tbl &lt;- (ggplot_build(p)$data[[1]])[, c("x", "count")]
  agg &lt;- aggregate(tbl["count"], by=tbl["x"], FUN=sum)
  
  for(i in 1:length(agg$x))
    if(agg$count[i])
      p &lt;- p + geom_text(x=agg$x[i], y=agg$count[i] + 1.5, label=agg$count[i], colour="black" )
  </code></pre>
  
  <p>which generates the following plot:</p>
  
  <p><a href="https://i.stack.imgur.com/6ft0Z.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6ft0Z.png" alt="enter image description here"></a> </p>
  
  <p>Is there a better (and more efficient) way to get the same result using ggplot2?
  Thanks a lot in advance</p>