Home > Back-end >  R, ggplot, combine 10 variables into one bar graph
R, ggplot, combine 10 variables into one bar graph

Time:10-13

I have data that I've extracted from a site called "Redcap", the data looks like this on Redcap: enter image description here

I.e. it was one "question" that respondents could pick multiple answers for, but they all got graphed together. To reiterate, the answers were NOT mutually exclusive.

Here's a sample of the de-identified data:

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

Because respondents could pick multiple answers, each answer is really its own variable and when exported, I get a column for each (Improvment_1 is the first row, improvement_2 the second, etc..). I'd like to create a graph, almost identical to the one I pasted, but faceted by the other variable I included: "AgeGroup". So it'd look like this (pardon my msPaint skills):

enter image description here

I'm struggling with how I would include 10 different "improvement" variables on the same graph.

CodePudding user response:

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)

enter image description here

CodePudding user response:

I admire your MS paint skills. Is this what you are looking for?

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))
  • Related