Add the number of observations per group in ggplot2 boxplot

Following this question: How to add the number of observations per group and use the average group value in ggplot2 boxplot? I want to add the number of observations per group in ggplot boxplot too. But I added color to aes mapping.

The existing answer shows how to adjust the position of the text along the y axis. How to change the position of the text along the x axis?

This is a minimal example to reproduce my problem:

library(ggplot2) give.n <- function(x){ return(c(y = median(x)*1.05, label = length(x))) # experiment with the multiplier to find the perfect position } p <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) + geom_boxplot() + stat_summary(fun.data = give.n, geom = "text", fun.y = median) p 

enter image description here

Thanks for any suggestions.

+7
r ggplot2
source share
2 answers

You can simply use position :

 p <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) + geom_boxplot() + stat_summary(fun.data = give.n, geom = "text", fun.y = median, position = position_dodge(width = 0.75)) p 

enter image description here

The argument width position_dodge() controls the positioning on the horizontal axis. 0.75 is a sweet spot, see. How it works for a different number of groups:

 p2 <- ggplot(mtcars, aes(factor(vs), mpg, colour = factor(cyl))) + geom_boxplot() + stat_summary(fun.data = give.n, geom = "text", fun.y = median, position = position_dodge(width = 0.75)) p2 

enter image description here

+7
source share

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)) 
+1
source share

All Articles