ggplot2 中的堆叠条形图,带有 2 个系列的条形图(不同的列)
Stacked bar in ggplot2, with 2 series of bars (different columns)
数据集:
我有如下数据集:http://www.sharecsv.com/s/786597b1557929e190edf69a46f1dec4/example.csv
三列,每列有 YES/NO 个答案。
问题:
我需要制作一个堆叠条形图,其中 A 是水平轴。我需要两个有两个并排的堆叠条(一个用于 B 列数据,一个用于 C 列数据)。
我尝试按如下方式对数据进行分组,但没有产生我需要的结果:
sum_data <- wsheet %>% group_by(A, B, C) %>% summarise(count = n())
我需要 B 和 C 仅根据 A 进行总结,以便能够将其放入情节中。
以下是我实现的不正确情节。对于 A 的每个值,除了 B 之外,我基本上还需要 C 的另一个堆叠列。
最近的尝试
我仍然没有得到我想要的图表,但这是我拥有的最接近的图表:
- 分组子数据集:
sub_b <- wsheet %>% group_by(A, B) %>% summarise(count = n())
colnames(sub_b) <- c("A", "value", "count")
sub_b$measure <- "B"
sub_c <- wsheet %>% group_by(A, C) %>% summarise(count = n())
colnames(sub_c) <- c("A", "value", "count")
sub_c$measure <- "C"
- 将它们绑定在一起:
sub_both <- rbind(sub_b, sub_c)
- 制作了多面图:
ggplot(sub_both) +
geom_bar( aes(x = A, y = count, fill = value), stat = "identity") +
facet_wrap(~measure)
我想要什么?
并排放置 B 和 C 条,而不是刻面。
是这样的吗?编辑为使用 facet_wrap
.
library(tidyverse)
wsheet <- data.frame(stringsAsFactors=FALSE,
A = c("YES", "YES", "NO", "YES", "NO", "YES", "NO", "YES"),
B = c("NO", "YES", "NO", "YES", "NO", "YES", "NO", "YES"),
C = c("YES", "YES", "NO", "YES", "YES", "NO", "YES", "NO"))
df <- wsheet %>%
pivot_longer(-A, names_to = "letter", values_to = "yn") %>%
group_by(letter) %>%
count(yn)
ggplot(df, aes(yn, n, fill = yn)) +
geom_col(position = "dodge") + facet_wrap(~letter) +
labs(x = NULL, fill = NULL) +
theme(legend.position = "none")
更新代码:
#Read data
wsheet <- read.csv('example.csv')
#Data transform
sub_b <- wsheet %>% group_by(A, B) %>% summarise(count = n())
colnames(sub_b) <- c("A", "value", "count")
sub_b$measure <- "B"
sub_c <- wsheet %>% group_by(A, C) %>% summarise(count = n())
colnames(sub_c) <- c("A", "value", "count")
sub_c$measure <- "C"
#Create variables
sub_both <- rbind(sub_b, sub_c)
sub_both$var <- paste0(sub_both$A,'.',sub_both$measure)
#Plot
ggplot(sub_both) +
geom_bar( aes(x = var, y = count, fill = value), stat = "identity")
数据集:
我有如下数据集:http://www.sharecsv.com/s/786597b1557929e190edf69a46f1dec4/example.csv
三列,每列有 YES/NO 个答案。
问题:
我需要制作一个堆叠条形图,其中 A 是水平轴。我需要两个有两个并排的堆叠条(一个用于 B 列数据,一个用于 C 列数据)。
我尝试按如下方式对数据进行分组,但没有产生我需要的结果:
sum_data <- wsheet %>% group_by(A, B, C) %>% summarise(count = n())
我需要 B 和 C 仅根据 A 进行总结,以便能够将其放入情节中。
以下是我实现的不正确情节。对于 A 的每个值,除了 B 之外,我基本上还需要 C 的另一个堆叠列。
最近的尝试
我仍然没有得到我想要的图表,但这是我拥有的最接近的图表:
- 分组子数据集:
sub_b <- wsheet %>% group_by(A, B) %>% summarise(count = n())
colnames(sub_b) <- c("A", "value", "count")
sub_b$measure <- "B"
sub_c <- wsheet %>% group_by(A, C) %>% summarise(count = n())
colnames(sub_c) <- c("A", "value", "count")
sub_c$measure <- "C"
- 将它们绑定在一起:
sub_both <- rbind(sub_b, sub_c)
- 制作了多面图:
ggplot(sub_both) +
geom_bar( aes(x = A, y = count, fill = value), stat = "identity") +
facet_wrap(~measure)
我想要什么?
并排放置 B 和 C 条,而不是刻面。
是这样的吗?编辑为使用 facet_wrap
.
library(tidyverse)
wsheet <- data.frame(stringsAsFactors=FALSE,
A = c("YES", "YES", "NO", "YES", "NO", "YES", "NO", "YES"),
B = c("NO", "YES", "NO", "YES", "NO", "YES", "NO", "YES"),
C = c("YES", "YES", "NO", "YES", "YES", "NO", "YES", "NO"))
df <- wsheet %>%
pivot_longer(-A, names_to = "letter", values_to = "yn") %>%
group_by(letter) %>%
count(yn)
ggplot(df, aes(yn, n, fill = yn)) +
geom_col(position = "dodge") + facet_wrap(~letter) +
labs(x = NULL, fill = NULL) +
theme(legend.position = "none")
更新代码:
#Read data
wsheet <- read.csv('example.csv')
#Data transform
sub_b <- wsheet %>% group_by(A, B) %>% summarise(count = n())
colnames(sub_b) <- c("A", "value", "count")
sub_b$measure <- "B"
sub_c <- wsheet %>% group_by(A, C) %>% summarise(count = n())
colnames(sub_c) <- c("A", "value", "count")
sub_c$measure <- "C"
#Create variables
sub_both <- rbind(sub_b, sub_c)
sub_both$var <- paste0(sub_both$A,'.',sub_both$measure)
#Plot
ggplot(sub_both) +
geom_bar( aes(x = var, y = count, fill = value), stat = "identity")