簡體   English   中英

如何 Plot 來自 Google 表單調查復選框網格問題的多個分類變量並在 R 中最佳可視化?

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

對 R 非常陌生,並且正在努力了解如何 plot 多個分類變量。 在一項調查中,有一個問題被格式化為復選框網格,因此受訪者能夠檢查多個項目的多個選項。 它看起來像這樣:

問題是了解您使用哪些應用程序以及如何使用它們? 我這里有所有的代碼。 實際上有 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"))

我創建的圖表

所以我有幾個問題:

  1. 有沒有辦法制作一個“單一”圖表,同時為每個應用程序顯示所有圖表? 我為我的意思插入了一張圖片我想要的示例圖表 還是我必須為所有 12 個代碼編寫代碼並運行它?
  2. 有沒有更好的方法來可視化這一點? 我考慮過馬賽克 plot 但我什至不確定我是否應該考慮它,或者我是否應該堅持使用普通條形圖。

任何幫助將不勝感激,因為我仍在導航如何在 R 中進行編碼。

一種方法可能是:

將所有數據框合並到一個my_data中。 為此,我們將所有以我們環境中的數據開頭的data框組合起來。 然后我們對每個單獨的 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