【问题标题】:Add sample size to a panel figure of boxplots将样本大小添加到箱线图的面板图中
【发布时间】:2017-09-18 22:36:19
【问题描述】:

我正在尝试将样本大小添加到按两个级别分组的箱线图(最好在它们的顶部或底部)。我使用 facet_grid() 函数生成面板图。然后我尝试使用 annotate() 函数来添加样本大小,但这不起作用,因为它重复了第二个面板中的值。有没有简单的方法来做到这一点?

head(FeatherData, n=10)
    Location   Status   FeatherD               Species        ID
## 1        TX Resident  -27.41495         Carolina wren CARW (32)
## 2        TX Resident  -29.17626         Carolina wren CARW (32)
## 3        TX Resident  -31.08070         Carolina wren CARW (32)
## 4        TX Migrant  -169.19579 Yellow-rumped warbler YRWA (28)
## 5        TX Migrant  -170.42079 Yellow-rumped warbler YRWA (28)
## 6        TX Migrant  -158.66925 Yellow-rumped warbler YRWA (28)
## 7        TX Migrant  -165.55278 Yellow-rumped warbler YRWA (28)
## 8        TX Migrant  -170.43374 Yellow-rumped warbler YRWA (28)
## 9        TX Migrant  -170.21801 Yellow-rumped warbler YRWA (28)
## 10       TX Migrant  -184.45871 Yellow-rumped warbler YRWA (28)


 ggplot(FeatherData, aes(x = Location, y = FeatherD)) +
   geom_boxplot(alpha = 0.7, fill='#A4A4A4') +
   scale_y_continuous() +
   scale_x_discrete(name = "Location") +
   theme_bw() +
   theme(plot.title = element_text(size = 20, family = "Times", face = 
 "bold"),
         text = element_text(size = 20, family = "Times"),
         axis.title = element_text(face="bold"),
         axis.text.x=element_text(size = 15)) +
   ylab(expression(Feather~delta^2~H["f"]~"‰")) +
   facet_grid(. ~ Status)

【问题讨论】:

  • 您能否发布您的数据示例 (head(FeatherData))?
  • 添加了示例以及当前的箱线图!

标签: ggplot2 rstudio boxplot


【解决方案1】:

有多种方法可以完成此类任务。最灵活的方法是在绘图调用之外计算您的统计数据作为单独的数据框并将其用作自己的图层:

library(dplyr)
library(ggplot2)

cw_summary <- ChickWeight %>% 
  group_by(Diet) %>% 
  tally()

cw_summary
# A tibble: 4 x 2
    Diet     n
  <fctr> <int>
1      1   220
2      2   120
3      3   120
4      4   118
ggplot(ChickWeight, aes(Diet, weight)) + 
  geom_boxplot() +
  facet_grid(~Diet) +
  geom_text(data = cw_summary,
            aes(Diet, Inf, label = n), vjust = 1)

另一种方法是使用内置的汇总函数,但这可能很繁琐。这是一个例子:

ggplot(ChickWeight, aes(Diet, weight)) + 
  geom_boxplot() +
  stat_summary(fun.y = median, fun.ymax = length,
               geom = "text", aes(label = ..ymax..), vjust = -1) +
  facet_grid(~Diet)

这里我使用fun.y 将摘要定位在y 值的中间位置,并使用fun.ymax 计算一个名为..ymax.. 的内部变量,函数length (只计算观察次数)。

【讨论】:

  • 谢谢@Brian!您的第一个建议效果很好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-09
  • 1970-01-01
相关资源
最近更新 更多