ggplot2 将 facet_wrap 与频率计数的多个堆叠条形图一起使用

ggplot 2 use facet wrap with multiple stacked barplots of frequency counts

我有许多二进制变量,我想根据条件表达行为发生的频率(1 = 存在,0 = 不存在)。我可以使用以下语法为单个变量成功绘制此图:

require(tidyverse)
require(ggplot2)
require(ggsignif)
require(ggpubr)


condition <- c("a", "a", "a", "b", "b", "b", "c", "c", "c", "c")
binary_1 <- c(0,0,0,0,0,1,1,1,1,1)
binary_2 <- c(1,1,1,1,1,1,0,0,0,0)
binary_3 <- c(0,1,1,1,1,1,1,1,0,0)
binary_4 <- c(1,1,1,0,0,0,0,0,0,0)


df <- data.frame(condition, binary_1, binary_2, binary_3, binary_4)
df

gg_df <- df %>%
  mutate(binary_1 = as.factor(binary_1), binary_2 = as.factor(binary_2), binary_3 = as.factor(binary_3), binary_4 = as.factor(binary_4))

gg_melt <- melt(gg_df)

# example for one of the variables (binary_1), I just swap the variable out for each graph
gg_1 <- ggplot(gg_melt, aes(x=condition, fill = binary_1)) +
  geom_bar(stat="count") +
  scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Behaviour Observed", labels = c("0" = "Absent", "1" = "Present")) +
  scale_x_discrete(labels = c(a = "Condition A", b = "Condition B", c = "Condition C")) + 
  xlab("Condition") + 
  ylab("Number of Participants") +
  theme(aspect.ratio = 1)

但是,由于所有变量都具有相同的 x 轴(条件)和 y 轴(频率计数),我希望使用 facet-wrap 在同一个图表上同时表达所有变量。但是,我似乎无法让它工作。我通读了以下主题:

percentage on y lab in a faceted ggplot barchart?

但我不知道如何按变量而不是计数数据进行拆分。

# Attempt 1
gg_df %>% 
  group_by(condition) %>%
  mutate(beh_count = n()) %>%
  ungroup() %>%
  mutate(beh_updated = paste0(condition, "; n=", beh_count)) %>%
  ggplot(aes(x = condition)) + geom_bar()+
  facet_wrap(~beh_updated)

# Attempt 2
gg_df %>% 
  ggplot(aes(x = condition)) + geom_bar()+
  facet_wrap(~binary_1 + binary_2 + binary_3 + binary_4)

# Attempt 3
ggplot(data = gg_df) + 
  geom_bar(aes(condition)) + 
  facet_wrap(~binary_1 + binary_2 + binary_3 + binary_4)

如何创建包含计数数据和变量的数据框才能正确使用分面换行?

这可以通过重塑数据使四个二进制变量成为一个变量的类别来实现。为此,我使用 tidyr::pivot_longer 而不是 reshape2::melt。重塑后,您可以 facet_wrap 通过新变量:

library(ggplot2)
library(tidyr)
library(dplyr)
gg_df <- df %>%
   mutate(across(starts_with("binary"), as.factor))

gg_melt <- tidyr::pivot_longer(gg_df, -condition, names_to = "binary")

ggplot(gg_melt, aes(x=condition, fill = value)) +
  geom_bar(stat="count") +
  scale_fill_manual(values = c("#FDAE61", "#9E0142"), name = "Behaviour Observed", labels = c("0" = "Absent", "1" = "Present")) +
  scale_x_discrete(labels = c(a = "Condition A", b = "Condition B", c = "Condition C")) + 
  xlab("Condition") + 
  ylab("Number of Participants") +
  theme(aspect.ratio = 1) +
  facet_wrap(~binary)