将样本大小添加到 ggplot boxplot

Adding sample size to ggplot boxplot

我有兴趣了解年龄与连续结果的关系,对此我有以下数据:

library(dplyr)
library(tidyverse)
library(magrittr)
library(ggplot2)

mydata <- 
structure(list(ID = c(104, 157, 52, 152, 114, 221, 320, 125, 
75, 171, 80, 76, 258, 82, 142, 203, 37, 92, 202, 58, 194, 38, 
4, 137, 25, 87, 40, 117, 21, 255, 277, 315, 96, 134, 185, 94, 
3, 153, 172, 65, 279, 209, 60, 13, 154, 160, 24, 29, 159, 213, 
127, 74, 48, 126, 184, 132, 61, 141, 27, 49, 8, 39, 164, 162, 
34, 205, 179, 119, 77, 135, 138, 165, 103, 253, 14, 20, 310, 
84, 30, 273, 22, 105, 262, 116, 86, 83, 145, 31, 95, 51, 81, 
271, 36, 50, 189, 2, 115, 7, 197, 54), age = c(67.1, 70.7, 53, 
61.7, 66.1, 57.7, 54.1, 67.2, 60.9, 55.8, 40.7, 57.6, 64.1, 70.7, 
47.5, 46.3, 66.7, 55, 63.3, 68.2, 61.2, 60.5, 52, 65.3, 48.9, 
56.9, 62.7, 75.2, 61.4, 57.9, 53.6, 58.1, 51, 67.3, 63.9, 57, 
43.2, 64.7, 62.8, 56.3, 51.7, 39.4, 45.2, 57.8, 55.7, 69.6, 61.5, 
50.1, 73.7, 55.5, 65.2, 54.6, 49, 35.2, 52.9, 46.3, 55, 52.5, 
54.2, 61, 57.4, 56.5, 53.6, 47.7, 64.2, 53.4, 60.9, 58.2, 60.7, 
50.3, 48.3, 74.7, 52.1, 59.9, 52.4, 70.8, 61.2, 66.5, 55.4, 57.5, 
59.2, 60.1, 52.3, 60.2, 54.8, 36.3, 61.5, 48.6, 56, 62, 64.8, 
40.4, 68.3, 60, 69.1, 56.6, 45.3, 58.5, 52.3, 52), continuous_outcome = c(3636.6, 
1128.2, 2007.5, 802.9, 332.3, 2636.1, 169.5, 67.9, 3261.8, 1920.3, 
155.2, 1677.2, 198.2, 11189.7, 560.9, 633.1, 196.1, 13.9, 100.7, 
7594.5, 1039.8, 83.9, 2646.8, 284.6, 306, 1135.6, 1883.1, 5681.4, 
1706.2, 2241.1, 97.7, 1106.8, 1107.1, 290.8, 2123.4, 267, 115.3, 
138.5, 152.7, 1338.9, 6709.8, 561.7, 1931.7, 3112.4, 1876.3, 
3795.9, 5706.7, 7.4, 1324.9, 4095.4, 205.4, 1886, 177.3, 304.4, 
1319.1, 415.9, 537.2, 3141.1, 740, 1976.7, 624.8, 983.1, 1163.5, 
1432.6, 3730.4, 2023.4, 498.2, 652.5, 982.7, 1345.3, 138.4, 1505.1, 
3528.1, 11.9, 884.5, 10661.6, 1911.4, 2800.8, 81.5, 396.4, 409.1, 
417.3, 186, 1892.4, 1689.7, 0, 210.1, 210.5, 3484.5, 3196.8, 
57.2, 20.2, 947, 540, 1603.1, 1571.8, 9.1, 149.2, 122, 63.2), 
    age_decades = structure(c(3L, 4L, 2L, 3L, 3L, 2L, 2L, 3L, 
    3L, 2L, 1L, 2L, 3L, 4L, 1L, 1L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 
    3L, 1L, 2L, 3L, 4L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 1L, 3L, 
    3L, 2L, 2L, 1L, 1L, 2L, 2L, 3L, 3L, 2L, 4L, 2L, 3L, 2L, 1L, 
    1L, 2L, 1L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 1L, 3L, 2L, 3L, 2L, 
    3L, 2L, 1L, 4L, 2L, 2L, 2L, 4L, 3L, 3L, 2L, 2L, 2L, 3L, 2L, 
    3L, 2L, 1L, 3L, 1L, 2L, 3L, 3L, 1L, 3L, 2L, 3L, 2L, 1L, 2L, 
    2L, 2L), .Label = c("1", "2", "3", "4"), class = "factor")), row.names = c(NA, 
-100L), class = c("tbl_df", "tbl", "data.frame"))

为了在 x 轴上绘制几十年的箱线图和我使用的连续结果 ggplot2。 我想做几个,并自动在 x 轴刻度上绘制样本大小。为此,我按如下方式计算了数据集中的标签:

mydata <- 
  mydata %>% 
  group_by(age_decades) %>% 
  mutate(n_decades=as_character(n())) %>% 
  mutate(label_decades=case_when(age_decades==1 ~ "Below 50", 
                                 age_decades==2 ~ "Between 50 and 60", 
                                 age_decades==3 ~ "Between 60 and 70", 
                                 age_decades==4 ~ "Above 70")) %>% 
  mutate(label_decades=paste0(label_decades, '\n n = ', n_decades)) %>% 
  ungroup() %>% 
  relocate(age_decades, label_decades, .after=age) %>% 
  select(-n_decades) %>% 
  arrange(ID)

然后我尝试使用新创建的变量 label_decades 来标记箱线图。我尝试的第一件事是:

ggplot(mydata, aes(x=age_decades, y=continuous_outcome)) + 
  geom_boxplot() + 
  scale_x_discrete(labels=mydata$label_decades)

但这只是绘制了数据集中出现的前几个标签(因此它们不对应于实际的箱线图):

然后我尝试了:

ggplot(mydata, aes(x=age_decades, y=continuous_outcome)) + 
  geom_boxplot() + 
  geom_text(data=mydata, aes(age_decades, Inf, label=label_decades), 
            vjust = 15, size=4)

效果更好,但字体真的很奇怪,而且原始 x 轴 labels/ticks 仍然显示。 任何人都知道如何解决这个问题?谢谢!

一种方法是将标签也转换为因子。

library(dplyr)
library(ggplot2)

mydata <- mydata %>% 
  group_by(age_decades) %>% 
  mutate(n_decades= as.character(n())) %>% 
  mutate(label_decades= case_when(age_decades==1 ~ "Below 50", 
                                 age_decades==2 ~ "Between 50 and 60", 
                                 age_decades==3 ~ "Between 60 and 70", 
                                 age_decades==4 ~ "Above 70")) %>% 
  mutate(label_decades= factor(paste0(label_decades, '\n n = ', n_decades))) %>% 
  ungroup() %>% 
  relocate(age_decades, label_decades, .after=age) %>% 
  select(-n_decades) %>% 
  arrange(ID)

然后您可以在 scale_x_discrete 中使用它的 levels

ggplot(mydata, aes(x=age_decades, y=continuous_outcome)) + 
  geom_boxplot() + 
  scale_x_discrete(labels= levels(mydata$label_decades))

字体看起来有线,因为有许多标签将相同的文本打印在彼此之上。您可以使用 distinct 每个 x 刻度仅获取一个标签,并使用 theme 函数去除 x 个刻度标签:

mydata %>%
  ggplot(aes(age_decades, continuous_outcome)) +
    geom_boxplot() +
    geom_text(
      data = mydata %>% distinct(age_decades, label_decades),
      mapping = aes(label = label_decades),
      y = 9e3
    ) +
    theme(
      axis.text.x = element_blank()
    )