Reputation: 29
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"))
So I have a few questions:
Any help would be much appreciated on this, as I still navigate how to code in R.
Upvotes: 2
Views: 184
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"))
Upvotes: 2