【问题标题】:R, ggplot, combine 10 variables into one bar graphR,ggplot,将10个变量组合成一个条形图
【发布时间】:2021-10-12 20:44:37
【问题描述】:

我有从名为“Redcap”的网站提取的数据,Redcap 上的数据如下所示:

即这是一个受访者可以选择多个答案的“问题”,但它们都被绘制在一起。重申一下,答案并不相互排斥。

以下是去识别化数据的示例:

df<-structure(list(improvement___1 = c(1, 0, 1, 1, 1, 1, 1, 1, 0, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), improvement___2 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 
0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
0, 0, 1), improvement___3 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 
1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0), improvement___4 = c(1, 
0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 
1, 1, 1), improvement___5 = c(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 
0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0), improvement___6 = c(0, 
1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 
0, 1, 1), improvement___7 = c(0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 
0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 
1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0), improvement___8 = c(0, 
1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 
0, 1, 1), improvement___9 = c(1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 
1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 
1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1), improvement___10 = c(0, 
0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), AgeGroups = structure(c(2L, 1L, 2L, 1L, 2L, 1L, 3L, 
1L, 2L, 2L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 1L, 2L, 2L, 1L, 
2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 
3L, 1L, 2L, 3L, 1L, 2L, 1L), .Label = c("Young", "Middle", "Old"
), class = "factor")), row.names = c(NA, -46L), class = c("tbl_df", 
"tbl", "data.frame"))

因为受访者可以选择多个答案,每个答案实际上是它自己的变量,当导出时,我得到每个答案的列(Improvment_1 是第一行,improvment_2 第二行,等等)。我想创建一个图表,与我粘贴的几乎相同,但由我包含的另一个变量构成:“AgeGroup”。所以它看起来像这样(请原谅我的 msPaint 技能):

我正在为如何在同一张图上包含 10 个不同的“改进”变量而苦苦挣扎。

【问题讨论】:

    标签: r ggplot2 redcap


    【解决方案1】:

    我很欣赏你的 MS 绘画技巧。这是你要找的吗?

    library(tidyverse)
    df_new <- df %>% 
      pivot_longer(-AgeGroups, "improvement")
    
    ggplot(df_new) +
      aes(x = improvement, weight = value) +
      geom_bar(fill = "#112446") +
      coord_flip() +
      theme_minimal() +
      facet_wrap(vars(AgeGroups))
    

    【讨论】:

      【解决方案2】:
      library(tidyverse)
      df %>%
          pivot_longer(-AgeGroups) %>%
          mutate(name = fct_inorder(name) %>% fct_rev) %>%
          count(AgeGroups, name, wt = value) %>%
          ggplot(aes(n, name)) +
          geom_col() +
          facet_wrap(~AgeGroups)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        • 2020-11-10
        • 1970-01-01
        • 2018-09-27
        • 1970-01-01
        相关资源
        最近更新 更多