简体   繁体   English

如何 Plot 来自 Google 表单调查复选框网格问题的多个分类变量并在 R 中最佳可视化?

[英]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"))

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?还是我必须为所有 12 个代码编写代码并运行它?
  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.我考虑过马赛克 plot 但我什至不确定我是否应该考虑它,或者我是否应该坚持使用普通条形图。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM