4个变量,其中3个具有2组,在RStudio中具有重叠的条形图-男性和女性已被接受,但未被接受

时间:2018-08-14 18:00:44

标签: r ggplot2

enter link description here我遇到了一个问题。我想在RStudio中绘制所有四个变量。在这里,我似乎有2组3个变量和一个Count。但是,不知道如何使用ggplot2执行此操作。在xlim轴上应为age_band和sex。在y轴上计数被录取和未被录取的人数。我希望图例在叠加的条形图下面。有人可以帮忙吗?我在stackoverflow上进行了搜索,找不到合适的可重复代码。

 dput(head(ds_sum_age_sex, 16))
 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"
             ))

2 个答案:

答案 0 :(得分:1)

这是我30秒的尝试:

onChangeHandler =(e)=> {
let cellProperties = [...this.state.cellProperties];
cellProperties[e.target.name] = e.target.value;
this.setState({cellProperties});}

something attempted

答案 1 :(得分:0)

我手动编辑了您的dput()数据,然后使用facet_wrap进行了尝试,

library(dplyr)
library(ggplot2)

test_data %>%
  group_by(age_band, sex, patient.class) %>%
  summarise(total_count = sum(Count)) %>%
  ggplot(data = ., aes(age_band, total_count)) +
  geom_bar(aes(fill = patient.class), stat = "identity") +
  facet_wrap(~ sex, nrow = 2, ncol = 1)

enter image description here