I can add the number of observations in a boxplot like this: Add number of observations per group in ggplot2 boxplot
Is it possible to add the relative frequencies? On the example above, I would add 0.66, 0.33, 0.5 and 0.5 texts instead of 12, 6, 7, 7.
So far I've managed to compute the absolute frequency of the top factor:
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
}
ggplot(mtcars, aes(factor(vs), mpg, colour = factor(am))) +
geom_boxplot() +
stat_summary(fun.data = give.n, geom = "text", fun.y = median) +
stat_summary(aes(factor(vs), mpg, colour = factor(am)), inherit.aes = F, fun.data = give.n, geom = "text", fun.y = median)
Thanks