Instead of stat_summary you can use geom_text. Please refer to the following question: ggplot2 add text on top of boxes .
This is an example of how you can do this with a number of observations:
# Create an aggregate of median & count > cts <- merge(aggregate(mpg ~ cyl + am, mtcars, length), aggregate(mpg ~ cyl + am, mtcars, median), by=c("cyl", "am")) # Rename the col names to fit with the original dataset.. > names(cts) <- c("cyl", "am", "count", "mpg") # As alexwhan suggested, position_dodge helps with positioning # along the x-axis.. > ggplot(mtcars, aes(factor(cyl), mpg, colour = factor(am))) + geom_boxplot(position = position_dodge(width=1.0)) + geom_text(data = cts, aes(label=count), position=position_dodge(width=1.0))
szabad
source share