modify stat_summary to also show count in distribution for box plot
<p>I have the boxplot below and I'd like to have the count as text. </p>

<pre><code>group = c( rep(c(1,2,1),100)   )
group2 = rep(c("A","B","B"),100)
r = rnorm(300,50,63)
d = data.frame(  group = group,  r = r  , group2 = group2)
head(d)
custom= function (x){
  q = quantile(x)
  return(       c(q[1],q[3],q[5])  )}

ggplot(data = d, aes(factor(group), r)) + 
  geom_boxplot()    +
  stat_summary(geom="text", fun.y=custom,
               aes(label=sprintf("%1.0f", ..y..)),
               position=position_nudge(x=0.33), size=3)+
  geom_text(data=      data.frame(x =  unique(as.factor(group))  , y = quantile(r[group])[3], label= paste0( "N=", length(r))   ),
            aes(x,y,label=label),inherit.aes = FALSE)+
  facet_wrap(~group2)
</code></pre>

<p><a href="https://i.stack.imgur.com/h6idC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/h6idC.png" alt="enter image description here"></a></p>

<p>Currently the count is the same for each box plot.  how do I make it accuratly reflect the count in each box plot AND to not print when there is no boxplot like in group2 =A where there is no boxplot but count is still printed?</p>

<p>Thank you.</p>