r - 如何从 Google 表单调查复选框网格问题中绘制多个分类变量并在 R 中进行最佳可视化?

标签 r ggplot2 bar-chart categorical-data mosaic-plot

对 R 非常陌生,正在努力理解如何绘制多个分类变量。在一项调查中,有一个问题被格式化为复选框网格,因此受访者能够检查多个项目的多个选项。它看起来像这样:

问题是了解您使用哪些应用程序以及如何使用它们?我这里有所有的代码。实际上,人们有 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"))

这是我使用下面的代码为其中一个绘制的图表。

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

所以我有几个问题:

  1. 有没有办法制作一个“单一”图表,让每个应用程序同时显示所有图表?我插入了一张图片来表达我的意思 Example chart of what I'd like 。或者我必须为所有 12 个代码编写代码并运行它?
  2. 有没有更好的方法来可视化这一点?我考虑过马赛克图,但我什至不确定我是否应该考虑它,或者我是否应该坚持使用普通的条形图。

对此我将不胜感激,因为我仍在导航如何在 R 中进行编码。

最佳答案

一种方法可以是:

将所有数据帧合并到一个my_data。为此,我们在我们的环境中组合了以 data 开头的所有数据帧。然后我们使用facet_wrap来绘制每个单独的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

关于r - 如何从 Google 表单调查复选框网格问题中绘制多个分类变量并在 R 中进行最佳可视化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73264340/

相关文章:

图像 r 中的反转 Axis

r - 通过垂直分割垃圾箱,通过精确的截止点为 ggplot 直方图着色

r - ggplot geom_bar : stack and center

android - 如何正确显示AChartEngine条形图

python - 将 3D 条形图置于 matplotlib 中给定位置的中心

r - 通过 data.table 或 dplyr 中的分组列选择每个数字列的最大绝对值

r - 按组查找特定日期上下最接近的值

r - R 中 xyplot() 中 x 轴上的值

r - 使用ggplot绘制趋势线

r - qqplot + stat_smooth 错误