0

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?

Forklift17
  • 2,245
  • 3
  • 20
  • 32

1 Answers1

1

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)))
}
Stibu
  • 15,166
  • 6
  • 57
  • 71