ilc17
ilc17

Reputation: 29

How to Plot Multiple Categorical Variables From a Google Form Survey Checkbox Grid Question and Best Visualize It in R?

very new to R and am struggling to understand how to plot multiple categorical variables. From a survey there was a question that was formatted in a checkbox grid, therefore respondents were able to check multiple options for multiple items. It looks something like this:

The question is getting at what apps do you use and how do you use them? I have the code for all of it down here. Realistically there are 12 apps people to choose from but I'm just showing 4 for these purposes.

data1 <- data.frame(apps = c("Education", "Leisure", "Research","Shopping",
                             "Business", "Organization", "Reporting"),
                    yes = c("5", "6","2","1","2","1","1"), 
                    no = c("6", "5","9","10","9","10","10"))

data2 <- data.frame(apps = c("Education", "Leisure", "Research","Shopping",
                             "Business", "Organization", "Reporting"),
                    yes = c("6", "6","3","1","2","1","1"), 
                    no = c("5", "5", "8", "10", "9","10","10"))

data3 <- data.frame(apps = c("Education", "Leisure", "Research","Shopping",
                             "Business", "Organization", "Reporting"), 
                    yes = c("6", "6","3","1","2","1","1"), 
                    no = c("5", "5", "8", "10", "9","10","10"))

data4 <- data.frame(apps = c("Education", "Leisure", "Research","Shopping",
                             "Business", "Organization", "Reporting"), 
                    yes = c("4", "4","2","2","3","1","1"), 
                    no = c("7", "7", "9", "9", "8","10","10"))

This is the chart I've come up with for ONE of them using the code below.

data1 %>% 
  mutate(
    yes = as.numeric(yes),
    no = as.numeric(no)
  ) %>%
  gather(key = "success", value=value, -apps) %>%
  ggplot(aes(x=apps, y=value, fill=success)) + 
  geom_bar(position = "stack", stat = "identity") +
  labs(title = "Appliations for App Use", x= "Applications", y= "# of individuals", fill = "Legend")+
  scale_fill_manual(values=c("purple", "pink"))

Chart I've Created

So I have a few questions:

  1. Is there a way to make a "single" chart that shows up all at the same time for each app? I've inserted a picture for what I mean Example chart of what I'd like. Or will I have to write the code for all 12 and run it?
  2. Is there a better way to visualize this? I've thought about a mosaic plot but I'm not even sure if I should even be considering it or if I should just stick with normal bar graphs.

Any help would be much appreciated on this, as I still navigate how to code in R.

Upvotes: 2

Views: 184

Answers (1)

TarJae
TarJae

Reputation: 79174

One way to do this could be:

Combine all of your dataframes to one my_data. For this here we combine all dataframes that start with data in our environment. Then we use facet_wrap to plot by each individual df:

library(tidyverse)
my_data <- bind_rows(mget(grep(pattern = "^[data]", x = ls(), 
                                 value = TRUE)), .id = 'filename') %>% 
  mutate(
    yes = as.numeric(yes),
    no = as.numeric(no)
  ) %>%
  pivot_longer(c(yes, no), names_to = "success") %>% 
  ggplot(aes(x=apps, y=value, fill=success)) + 
  geom_bar(position = "stack", stat = "identity") +
  facet_wrap(.~filename, scales = "free_x")+
  labs(title = "Appliations for App Use", x= "Applications", y= "# of individuals", fill = "Legend")+
  scale_fill_manual(values=c("purple", "pink"))

enter image description here

Upvotes: 2

Related Questions