R-使用ggplot在二因子箱线图中写入相对频率

时间:2018-09-12 00:28:34

标签: r ggplot2

我可以在箱形图中添加观测数量,例如: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)

谢谢

0 个答案:

没有答案