R,ggplot,将 10 个变量组合成一个条形图

R, ggplot, combine 10 variables into one bar graph

我有从名为“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 是第一行,improvement_2 第二行,依此类推。 .).我想创建一个图表,几乎与我粘贴的图表相同,但包含我包含的另一个变量:“AgeGroup”。所以它看起来像这样(请原谅我的 msPaint 技能):

我正在努力解决如何在同一个图表上包含 10 个不同的“改进”变量。

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)

我很欣赏你的 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))