r - 绘制 4 组 - 女性录取和 Not_Admitted,男性录取和不录取 ggplot2

标签 r ggplot2

我遇到了一个问题。我想在 RStudio 中绘制所有四个变量。我似乎有 2 个组用于 3 个变量和一个计数。但是不知道如何使用 ggplot2 执行此操作。在 xlim 轴上应为 age_band 和 sex。在 y 轴上,被录取和未被录取的人数。我想要叠加的条形图下面的图例。由于分析和数据的保密性,我在下面添加了绘制的图片。有人可以帮忙吗?我在 stackoverflow 上搜索过,找不到好的可重现代码 .

这是我在处理技术后拥有的 2 种类型的数据。

第一类数据:

 structure(list(age_band = c("0 yrs", "0 yrs", "0 yrs", "0 yrs", 
                       "1-4 yrs", "1-4 yrs", "1-4 yrs", "1-4 yrs", 
                     "10-14 yrs", "10-14 yrs", "10-14 yrs", "10-14 yrs",                              
                      "15-19 yrs", "15-19 yrs", "15-19 yrs","15-19 yrs"), 
            sex = c("Female", "Female", "Male", "Male", "Female", 
                     "Female", "Male", "Male", "Female", "Female", 
                    "Male", "Male", "Female", "Female", "Male", "Male"), 
            patient.class = c("Not Admitted", "ORDINARY ADMISSION", 
                              "Not Admitted", "ORDINARY ADMISSION", "Not 
                               Admitted", "ORDINARY ADMISSION", "Not 
                               Admitted", "ORDINARY ADMISSION", 
                               "Not Admitted", "ORDINARY ADMISSION", "Not 
                                Admitted", "ORDINARY ADMISSION", "Not 
                               Admitted", "ORDINARY ADMISSION", 
                               "Not Admitted", "ORDINARY ADMISSION"), 
            Count = c(5681L, 1458L, 7667L, 2154L, 8040L, 2481L, 11737L, 
                      3601L, 2904L, 938L, 3883L, 1233L, 3251L, 1266L, 
                      2465L, 1031L)), 
            row.names = c(NA, -16L), class = c("tbl_df", "tbl", 
           "data.frame"
         ))

第二种数据:

   structure(list(age_band = c("0 yrs", "0 yrs", "0 yrs", "0 yrs", 
                               "1-4 yrs", "1-4 yrs", "1-4 yrs", "1-4 yrs", 
                               "10-14 yrs", "10-14 yrs", 
                               "10-14 yrs", "10-14 yrs", "15-19 yrs", 
                               "15- 19 yrs", "15-19 yrs", "15-19 yrs"), 
         sex_patient_class = c("female_admitted", "female_not_admitted", 
                                "male_admitted", "male_not_admitted", 
                               "female_admitted", "female_not_admitted", 
                               "male_admitted", "male_not_admitted", 
                               "female_admitted", "female_not_admitted", 
                               "male_admitted", "male_not_admitted", 
                               "female_admitted", "female_not_admitted", 
                               "male_admitted", "male_not_admitted"), 
         Count = c(1458L, 5681L,  2154L, 7667L, 2481L, 8040L, 3601L, 11737L, 
                   938L, 2904L, 1233L, 3883L, 1266L, 3251L, 1031L, 2465L)), 
         row.names = c(NA, -16L), class = c("grouped_df", "tbl_df", "tbl", 
                                            "data.frame"), 
        vars = "age_band", drop = TRUE, indices = list( 0:3, 4:7, 8:11, 
                                                        12:15), 
        group_sizes = c(4L, 4L, 4L, 4L), biggest_group_size = 4L, labels = 
        structure(list(age_band = c("0 yrs", "1-4 yrs", "10-14 yrs", "15-19 
                                     yrs")), 
         row.names = c(NA, -4L), class = "data.frame", vars = "age_band", 
         drop = TRUE))

最佳答案

要将入院患者的列叠加到未入院患者的列上,您可以通过两种方式过滤数据。我在开始时指定美学有一个共同的填充图例。

library(tidyverse)

ggplot(my_data2, aes(age_band, Count, fill = sex_patient_class)) +
  geom_col(data = filter(my_data2, sex_patient_class %in% c("male_not_admitted", "female_not_admitted")), 
           position = position_dodge()) +
  geom_col(data = filter(my_data2, sex_patient_class %in% c("male_admitted", "female_admitted")), 
           position = position_dodge(0.9), width = 0.5) +
  scale_fill_manual(name = "", 
                    breaks = c("male_admitted", "male_not_admitted", 
                               "female_admitted", "female_not_admitted"),
                    labels = c("Male Admitted", "Male Not admitted", 
                               "Female Admitted", "Female Not admitted"), 
                    values = c("grey80", "black", "red", "orange"))

enter image description here

详细解释

实际叠加发生在两个 geom_col 调用中。调用的顺序很重要,因为第二个调用位于第一个调用的上方。因此,我们从“后退”栏开始:

使用 filter,我们只选择 not_admitted 患者并将其用作 geom_col 的数据。我们不需要重复最初的 ggplot 调用的美学,因为如果没有另外指定,它们是继承的。 position_dodge() 将每个年龄段的列并排放置。

p <- ggplot(my_data2, aes(age_band, Count, fill = sex_patient_class)) +
  geom_col(data = filter(my_data2, sex_patient_class %in% c("male_not_admitted", "female_not_admitted")), 
           position = position_dodge()) 
p

enter image description here

现在要在顶部添加其他列,我们将过滤语句更改为入院患者。由于我们希望“前”列比“后”列窄,因此我们指定了 width=0.5

p + geom_col(data = filter(my_data2, sex_patient_class %in% c("male_admitted", "female_admitted")), 
             position = position_dodge(), width = 0.5)

enter image description here

现在我们快完成了。要将“前”列移动到“后”列的中心,我们需要指定 position_dodge() 的宽度。在这种情况下,为了使它们居中,该值为 0.9。要在“保存侧”(即确保确实在后列前面居中)为两个 geom_col 调用指定相同的闪避宽度。然后我们更改不太漂亮的颜色(这里使用啤酒调色板“配对”)和图例信息并完成:

p + geom_col(data = filter(my_data2, sex_patient_class %in% c("male_admitted", "female_admitted")), 
             position = position_dodge(0.9), width = 0.5) +
  scale_fill_brewer(name = "", 
                    breaks = c("male_admitted", "male_not_admitted", 
                               "female_admitted", "female_not_admitted"),
                    labels = c("Male Admitted", "Male Not admitted", 
                               "Female Admitted", "Female Not admitted"), 
                    palette = "Paired")

enter image description here

关于r - 绘制 4 组 - 女性录取和 Not_Admitted,男性录取和不录取 ggplot2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51875171/

相关文章:

r - 使用过滤器 dplyr 无法过滤 df 中的现有值

r - 数学函数的非数字参数

r - 如果变量中存在重复值,则根据其他变量保留具有最低值的行

r - 在边界框的边缘剪裁的 Voronoi 多边形

r - 如何在不知道情节的确切坐标的情况下将文本放在情节 (ggplot2) 上?

r - 一起使用facet_grid和facet_wrap

r - 如何使用多字节分隔符将文本文件读入 GNU R?

R ggplot 从图例中删除某些项目

r - 强制原点从 0 开始,在新的 ggplot 主题中,数据和 x 轴之间没有边距

r - 如何将scale_y_continuous(labels = scales::percent) 更改为