I am trying to do something like the first solution here:
give.n <- function(x){
return(c(y = mean(x), label = length(x)))
}
ggplot(diamonds, aes(cut, price)) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text")
But I want those sample sizes to be displayed at position y = 0, not in the boxes of the boxplots. How do I do this?
The function give.n()
returns both, the y-coordinate at which to display the number and the number to be displayed (i.e., the label). You simply need to change the y-coordinate returned by give.n()
:
give.n <- function(x){
return(c(y = 0, label = length(x)))
}