如何在 ggplot2 的闪避 geom_bar 中的类别集之间添加 space?
How to add space between sets of categories in a dodged geom_bar in ggplot2?
library(tidyverse)
library(ggsci)
DF <- tibble(Decision = sample(c("Negative","Positive"), 500, T),
Category1 = sample(c("X", "Y", "Z"), 500, T),
Category2 = sample(c("Yellow", "Blue", "Black", "White"), 500, T),
Category3 = sample(c("Xyz", "Yes", "Zos"), 500, T),
Category4 = sample(c("O", "F"), 500, T),
Category5 = sample(c("Xxx", "Yyy", "Zzz", "ooo", "Aha!"), 500, T))
我有一个包含五个不同问题的数据集,每个问题都有一组独特的答案。每个回答这五个问题的人都以肯定或否定的决定(第 6 个变量)结束。我创建了一个条形图,使用以下代码分别显示每个问题的每个答案的积极决定的百分比:
DF %>% pivot_longer(cols = 2:6, values_to = "Answer", names_to = "Category") %>%
count(Category, Decision, Answer) %>%
group_by(Category, Answer) %>% mutate(percent = n / sum(n) * 100) %>%
filter(Decision == "Positive") %>%
ggplot(aes(Answer %>% fct_reorder2(percent, Category), percent, fill = Category)) +
geom_bar(stat = "identity", position = position_dodge(), width = 0.95, color = "black", alpha = 0.5) + coord_flip() +
scale_fill_uchicago() + labs(x = "", y = "", fill = "") + scale_y_continuous(breaks = seq(0, 100, 20), labels = str_c(seq(0, 100, 20), "%")) +
theme_classic() + theme(legend.position = "top")
这是产品:
我的问题是 - 是否可以在每个问题的答案集之间添加一些 space?我希望相同颜色的列彼此相邻,但同时,我想在不同颜色的列之间添加一些 space ,以便在视觉上更清楚那些是 5不同的变量。
此外,如果可能的话,我想按降序分别为每个类别显示列。
不幸的是,将宽度添加到 position_dodge
position_dodge(0.5)
不起作用,我想这是有道理的。
如有任何帮助,我将不胜感激。提前致谢!
可以这样实现:
要在类别之间添加一些 space,您可以使用 facet_grid
,删除条形文本并将面板间距设置为零。此外,我使用 space="free"
以便您的栏仍然具有相同的宽度。
要按降序对条形图重新排序,您可以使用 tidytext:: reorder_within
和 tidytext::scale_x_reordered()
library(tidyverse)
library(ggsci)
library(tidytext)
set.seed(42)
DF <- tibble(
Decision = sample(c("Negative", "Positive"), 500, T),
Category1 = sample(c("X", "Y", "Z"), 500, T),
Category2 = sample(c("Yellow", "Blue", "Black", "White"), 500, T),
Category3 = sample(c("Xyz", "Yes", "Zos"), 500, T),
Category4 = sample(c("O", "F"), 500, T),
Category5 = sample(c("Xxx", "Yyy", "Zzz", "ooo", "Aha!"), 500, T)
)
DF %>%
pivot_longer(cols = 2:6, values_to = "Answer", names_to = "Category") %>%
count(Category, Decision, Answer) %>%
group_by(Category, Answer) %>%
mutate(percent = n / sum(n) * 100) %>%
filter(Decision == "Positive") %>%
ungroup() %>%
mutate(Answer = tidytext::reorder_within(Answer, by = percent, within = Category)) %>%
ggplot(aes(Answer, percent, fill = Category)) +
geom_bar(stat = "identity", position = position_dodge(), width = 0.9, color = "black", alpha = 0.5) +
coord_flip() +
scale_fill_uchicago() +
labs(x = "", y = "", fill = "") +
scale_y_continuous(breaks = seq(0, 100, 20), labels = str_c(seq(0, 100, 20), "%")) +
tidytext::scale_x_reordered() +
facet_grid(Category ~ ., scales = "free_y", space = "free") +
theme_classic() +
theme(legend.position = "top", strip.text = element_blank(), panel.spacing.y = unit(0, "pt"))
library(tidyverse)
library(ggsci)
DF <- tibble(Decision = sample(c("Negative","Positive"), 500, T),
Category1 = sample(c("X", "Y", "Z"), 500, T),
Category2 = sample(c("Yellow", "Blue", "Black", "White"), 500, T),
Category3 = sample(c("Xyz", "Yes", "Zos"), 500, T),
Category4 = sample(c("O", "F"), 500, T),
Category5 = sample(c("Xxx", "Yyy", "Zzz", "ooo", "Aha!"), 500, T))
我有一个包含五个不同问题的数据集,每个问题都有一组独特的答案。每个回答这五个问题的人都以肯定或否定的决定(第 6 个变量)结束。我创建了一个条形图,使用以下代码分别显示每个问题的每个答案的积极决定的百分比:
DF %>% pivot_longer(cols = 2:6, values_to = "Answer", names_to = "Category") %>%
count(Category, Decision, Answer) %>%
group_by(Category, Answer) %>% mutate(percent = n / sum(n) * 100) %>%
filter(Decision == "Positive") %>%
ggplot(aes(Answer %>% fct_reorder2(percent, Category), percent, fill = Category)) +
geom_bar(stat = "identity", position = position_dodge(), width = 0.95, color = "black", alpha = 0.5) + coord_flip() +
scale_fill_uchicago() + labs(x = "", y = "", fill = "") + scale_y_continuous(breaks = seq(0, 100, 20), labels = str_c(seq(0, 100, 20), "%")) +
theme_classic() + theme(legend.position = "top")
这是产品:
我的问题是 - 是否可以在每个问题的答案集之间添加一些 space?我希望相同颜色的列彼此相邻,但同时,我想在不同颜色的列之间添加一些 space ,以便在视觉上更清楚那些是 5不同的变量。
此外,如果可能的话,我想按降序分别为每个类别显示列。 不幸的是,将宽度添加到 position_dodge
position_dodge(0.5)
不起作用,我想这是有道理的。
如有任何帮助,我将不胜感激。提前致谢!
可以这样实现:
要在类别之间添加一些 space,您可以使用
facet_grid
,删除条形文本并将面板间距设置为零。此外,我使用space="free"
以便您的栏仍然具有相同的宽度。要按降序对条形图重新排序,您可以使用
tidytext:: reorder_within
和tidytext::scale_x_reordered()
library(tidyverse)
library(ggsci)
library(tidytext)
set.seed(42)
DF <- tibble(
Decision = sample(c("Negative", "Positive"), 500, T),
Category1 = sample(c("X", "Y", "Z"), 500, T),
Category2 = sample(c("Yellow", "Blue", "Black", "White"), 500, T),
Category3 = sample(c("Xyz", "Yes", "Zos"), 500, T),
Category4 = sample(c("O", "F"), 500, T),
Category5 = sample(c("Xxx", "Yyy", "Zzz", "ooo", "Aha!"), 500, T)
)
DF %>%
pivot_longer(cols = 2:6, values_to = "Answer", names_to = "Category") %>%
count(Category, Decision, Answer) %>%
group_by(Category, Answer) %>%
mutate(percent = n / sum(n) * 100) %>%
filter(Decision == "Positive") %>%
ungroup() %>%
mutate(Answer = tidytext::reorder_within(Answer, by = percent, within = Category)) %>%
ggplot(aes(Answer, percent, fill = Category)) +
geom_bar(stat = "identity", position = position_dodge(), width = 0.9, color = "black", alpha = 0.5) +
coord_flip() +
scale_fill_uchicago() +
labs(x = "", y = "", fill = "") +
scale_y_continuous(breaks = seq(0, 100, 20), labels = str_c(seq(0, 100, 20), "%")) +
tidytext::scale_x_reordered() +
facet_grid(Category ~ ., scales = "free_y", space = "free") +
theme_classic() +
theme(legend.position = "top", strip.text = element_blank(), panel.spacing.y = unit(0, "pt"))