![](/img/trans.png)
[英]How to visualize multiple categorical variables vs one continuous variable in 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"))
所以我有幾個問題:
任何幫助將不勝感激,因為我仍在導航如何在 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.