Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 图4组-女性入院和未入院,男性入院和未入院,ggplot2_R_Ggplot2 - Fatal编程技术网

R 图4组-女性入院和未入院,男性入院和未入院,ggplot2

R 图4组-女性入院和未入院,男性入院和未入院,ggplot2,r,ggplot2,R,Ggplot2,我面临一个问题。我想在RStudio中绘制所有四个变量。其中,我似乎有两组3个变量和一个计数。但我不知道如何使用ggplot2实现这一点。xlim轴上应显示年龄和性别。在y轴上,计算被接纳和未被接纳的人数。我想要覆盖条形图下面的图例。贝娄,由于分析和数据的保密性,我已经添加了绘制的图片。有人能帮忙吗?我在stackoverflow上搜索过,没有找到一个好的可复制代码 这里有两种类型的数据,都是经过处理后得到的 第一类数据: structure(list(age_band = c("0 yrs"

我面临一个问题。我想在RStudio中绘制所有四个变量。其中,我似乎有两组3个变量和一个计数。但我不知道如何使用ggplot2实现这一点。xlim轴上应显示年龄和性别。在y轴上,计算被接纳和未被接纳的人数。我想要覆盖条形图下面的图例。贝娄,由于分析和数据的保密性,我已经添加了绘制的图片。有人能帮忙吗?我在stackoverflow上搜索过,没有找到一个好的可复制代码

这里有两种类型的数据,都是经过处理后得到的

第一类数据:

 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))
第二类数据:

 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"))

详细说明

实际叠加发生在两个
geom\u col
调用中。调用的顺序很重要,因为第二个调用绘制在第一个调用的上方。因此,我们从“后退”列开始:

使用
filter
我们只选择未入院的患者,并将其用作
geom\u 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

现在我们差不多完成了。要将“前”列移动到“后”列的中心,我们需要指定
位置\u dodge()
的宽度。在本例中,要使其居中,该值为0.9。要处于“保存侧”(即确保后立柱前面确实居中),请为两个
geom\u col
-调用指定相同的减淡宽度。然后,我们更改不太漂亮的颜色(此处使用brewer调色板“配对”)和图例信息,并完成以下操作:

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")

嘿,凯丝,谢谢你。事实上,我希望女性在女性不被承认之前被承认,而男性在男性不被承认之前被承认。希望我的照片足够清晰。并提出了第二类数据,使人们更容易理解。你好,Kath-Henrick提供了帮助,我使用的语言似乎必须更加相关。谢谢你,凯丝。在中叠加/叠加分组条形图ggplot2@kath优雅的解决方案。每天访问此网站将“远离医生”。从学习的角度来看,您是否可以在回答中添加更多的文本,详细解释叠加是如何发生的。是的,我对答案投了赞成票。@nila,mbara,我补充了一个更长的解释。谢谢你的评论,通过修改这个问题,我还发现我的答案中有一些不必要的代码。所以我们都从中得到了一些东西;)@凯丝非常感谢你的详细解释。现在,我认为答案是完整的,参考了
丰富的学习经验
@gabriel burcea感谢您提出了一个
漂亮的
问题,并提供了一个
最小reprex
。相关:Henrik-这似乎是对的。多谢各位!