将摘要统计信息添加到两个因子框图中

时间:2017-12-28 18:57:19

标签: r ggplot2

我想在包含两个因素的箱线图中添加汇总统计数据(例如平均值)。我试过这个:

library(ggplot2)
ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
   stat_boxplot(geom = "errorbar", aes(col = supp, fill=supp), position = position_dodge(width = 0.85)) + 
   geom_boxplot(aes(col = supp, fill=supp), notch=T, notchwidth = 0.5, outlier.size=2, position =  position_dodge(width = 0.85)) +
   stat_summary(fun.y=mean, aes(supp,dose), geom="point", shape=20, size=7, color="violet", fill="violet") +
   scale_color_manual(name = "SUPP", values = c("blue", "darkgreen")) +
   scale_fill_manual(name = "SUPP", values = c("lightblue", "green"))

我得到了这张照片:

enter image description here

有可能以某种方式放置每个盒子的样本大小(例如胡须的顶部)?我试过这个:

ggplot(ToothGrowth, aes(x = factor(dose), y = len)) +
   stat_boxplot(geom = "errorbar", aes(col = supp, fill=supp), position = position_dodge(width = 0.85)) + 
geom_boxplot(aes(col = supp, fill=supp), notch=T, notchwidth = 0.5, outlier.size=2, position =  position_dodge(width = 0.85)) +
stat_summary(fun.y=mean,aes(supp,dose),geom="point", shape=20, size=7, color="violet", fill="violet") +
scale_color_manual(name = "SUPP", values = c("blue", "darkgreen")) +
scale_fill_manual(name = "SUPP", values = c("lightblue", "green")) +
geom_text(data = ToothGrowth,
          group_by(dose, supp),
          summarize(Count = n(),
                  q3 = quantile(ToothGrowth, 0.75),
                  iqr = IQR(ToothGrowth),
                  aes(x= dose, y = len,label = paste0("n = ",Count, "\n")), position = position_dodge(width = 0.75)))   

1 个答案:

答案 0 :(得分:1)

你可以通过将它们置于主ggplot调用中来陈述美学一次,然后它们将应用于所有geom层:ggplot(ToothGrowth, aes(x = factor(dose), y = len, color=supp, fill=supp))

对于观察次数:geom_text中的数据摘要步骤未正确编码。另外,要为文本展示位置设置len(y值),summarize函数需要输出len的值。

要在x轴上的正确位置添加平均值,请使用与其他geoms和stats完全相同的美学stat_summary。我通过将颜色设置为黄色来覆盖颜色审美,以便在框图填充颜色的顶部可以看到点标记。

实施情节的代码如下:

library(tidyverse)

pd = position_dodge(0.85)

ggplot(ToothGrowth, aes(x = factor(dose), y = len, color=supp, fill=supp)) +
  stat_boxplot(geom = "errorbar", position = pd) + 
  geom_boxplot(notch=TRUE, notchwidth=0.5, outlier.size=2, position=pd) +
  stat_summary(fun.y=mean, geom="point", shape=3, size=2, colour="yellow", stroke=1.5, 
               position=pd, show.legend=FALSE) +
  scale_color_manual(name = "SUPP", values = c("blue", "darkgreen")) +
  scale_fill_manual(name = "SUPP", values = c("lightblue", "green")) +
  geom_text(data = ToothGrowth %>% group_by(dose, supp) %>% 
              summarize(Count = n(),
                        len=max(len) + 0.05 * diff(range(ToothGrowth$len))),
            aes(label = paste0("n = ", Count)), 
            position = pd, size=3, show.legend = FALSE) +
  theme_bw()

enter image description here

请注意,凹槽位于所有箱形图的铰链外部。此外,样本大小刚好超过每个箱图的最大值似乎分散了我的注意力。您可以将所有文本注释放在图的底部,如下所示:

  geom_text(data = ToothGrowth %>% group_by(dose, supp) %>% 
              summarize(Count = n()) %>% 
              ungroup %>% 
              mutate(len=min(ToothGrowth$len) - 0.05 * diff(range(ToothGrowth$len))),
            aes(label = paste0("n = ", Count)), 
            position = pd, size=3, show.legend = FALSE) +