0

我有从名为“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 个不同的“改进”变量而苦苦挣扎。

4

2 回答 2

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

在此处输入图像描述

于 2021-10-12T20:50:45.953 回答
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))
于 2021-10-12T21:10:28.923 回答