[英]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.对 R 非常陌生,并且正在努力了解如何 plot 多个分类变量。 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.实际上有 12 个应用可供人们选择,但我只是出于这些目的展示了 4 个。
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.任何帮助将不胜感激,因为我仍在导航如何在 R 中进行编码。
One way to do this could be:一种方法可能是:
Combine all of your dataframes to one my_data
.将所有数据框合并到一个my_data
中。 For this here we combine all dataframes that start with data
in our environment.为此,我们将所有以我们环境中的数据开头的data
框组合起来。 Then we use facet_wrap
to plot by each individual df:然后我们对每个单独的 df 使用facet_wrap
到 plot:
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"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.