繁体   English   中英

在ggplot2 boxplot中为每个组和子组添加多个观察值

[英]Add a number of observations per group AND SUBGROUP in ggplot2 boxplot

这似乎是该问题的重复,但实际上我想扩展原始问题。

我想用ggplot中的每个组和SUBGROUP观察数来注释箱线图。 在该示例或原始帖子之后,这是我的最小示例:

require(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(cyl), mpg, fill = factor(gear))) +
  geom_boxplot() +
  stat_summary(fun.data = give.n, geom = "text", fun.y = median)

我的问题是样本数量全部排在组的中心,而不是在适当的箱线图中绘制(如下图所示): 注释在组中间居中,而不是在适当的箱线图中绘制

是你想要的吗?

require(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(cyl), mpg, fill = factor(gear))) +
  geom_boxplot() +
  stat_summary(fun.data = give.n, geom = "text", fun.y = median, position=position_dodge(width=0.75))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM