title: Use stat_summary to annotate plot with number of observations
body: <p>How can I use <code>stat_summary</code> to label a plot with <code>n = x</code> where is <code>x</code> a variable? Here's an example of the desired output:</p>
  
  <p><img src="https://i.stack.imgur.com/EX1ab.png" alt="enter image description here"></p>
  
  <p>I can make that above plot with this rather inefficient code: </p>
  
  <pre><code>nlabels &lt;- sapply(1:length(unique(mtcars$cyl)), function(i) as.vector(t(as.data.frame(table(mtcars$cyl))[,2][[i]])))
  ggplot(mtcars, aes(factor(cyl), mpg, label=rownames(mtcars))) +
    geom_boxplot(fill = "grey80", colour = "#3366FF") + 
    geom_text(aes(x = 1, y = median(mtcars$mpg[mtcars$cyl==sort(unique(mtcars$cyl))[1]]), label = paste0("n = ",nlabels[[1]]) )) +
    geom_text(aes(x = 2, y = median(mtcars$mpg[mtcars$cyl==sort(unique(mtcars$cyl))[2]]), label = paste0("n = ",nlabels[[2]]) )) +
    geom_text(aes(x = 3, y = median(mtcars$mpg[mtcars$cyl==sort(unique(mtcars$cyl))[3]]), label = paste0("n = ",nlabels[[3]]) )) 
  </code></pre>
  
  <p>This is a follow up to this question: <a href="https://stackoverflow.com/q/15660829/1036500">How to add a number of observations per group and use group mean in ggplot2 boxplot?</a> where I can use <code>stat_summary</code> to calculate and display the number of observations, but I haven't been able to find a way to include <code>n =</code> in the <code>stat_summary</code> output. Seems like <code>stat_summary</code> might be the most efficient way to do this kind of labelling, but other methods are welcome.</p>