我可以在箱形图中添加观测数量,例如:Add number of observations per group in ggplot2 boxplot
是否可以添加相对频率?在上面的示例中,我将添加0.66、0.33、0.5和0.5文本,而不是12、6、7、7。
到目前为止,我已经设法计算出最主要因素的绝对频率:
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)
谢谢