与 ggarrange 共享 x 和 y 轴标签 ggplot2

bec*_*bot 8 r ggplot2

我有一些基于二分计数数据创建的 ggplot 对象。我使用 ggarrange 将它们组合在一起。在此函数中,有一个选项可以创建共享图例,但据我所知,无法创建共享 x 和 y 轴标签。此外,图形的间距非常奇怪——两个图形列之间存在巨大的间隙,并且在共享图例之前还有大量的垂直空间。所以总而言之,我希望能够创建共享的 x 和 y 轴并最小化不必要的垂直和水平空间。

我检查了以下线程: ggplot2 grid_arrange_shared_legend share axis labels

ggplot:将图对齐并添加通用标签和图例

为 ggplot 中的多个绘图添加带有线条/箭头的公共轴标题

ggplot:如何将常见的 x 和 y 标签添加到绘图网格中

但我认为我想做的要简单一些,而且我不知道如何将 ggarrange 与facet.grid 结合起来,这已经被建议了几次。

我在下面复制了一个可重现的示例。

require(tidyverse)
require(ggpubr)
require(reshape2) 

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)

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") +
  ggtitle("Binary 1") +
  theme(plot.title = element_text(size = 10, face="bold", hjust = 0.5)) +
  theme(aspect.ratio = 1) +
  theme(legend.title=element_text(size=10, face = "bold"), axis.title = element_text(size=10, face = "bold"), axis.text = element_text(size=10))+
  theme(legend.box.just = "center")

gg_2 <- ggplot(gg_melt, aes(x=condition, fill = binary_2)) +
  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") +
  ggtitle("Binary 2") +
  theme(plot.title = element_text(size = 10, face="bold", hjust = 0.5)) +
  theme(aspect.ratio = 1) +
  theme(legend.title=element_text(size=10, face = "bold"), axis.title = element_text(size=10, face = "bold"), axis.text = element_text(size=10))+
  theme(legend.box.just = "center")

gg_3 <- ggplot(gg_melt, aes(x=condition, fill = binary_3)) +
  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") +
  ggtitle("Binary 3") +
  theme(plot.title = element_text(size = 10, face="bold", hjust = 0.5)) +
  theme(aspect.ratio = 1) +
  theme(legend.title=element_text(size=10, face = "bold"), axis.title = element_text(size=10, face = "bold"), axis.text = element_text(size=10))+
  theme(legend.box.just = "center")

gg_4 <- ggplot(gg_melt, aes(x=condition, fill = binary_4)) +
  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") +
  ggtitle("Binary 4") +
  theme(plot.title = element_text(size = 10, face="bold", hjust = 0.5)) +
  theme(aspect.ratio = 1) +
  theme(legend.title=element_text(size=10, face = "bold"), axis.title = element_text(size=10, face = "bold"), axis.text = element_text(size=10))+
  theme(legend.box.just = "center")

figure <- ggarrange(gg_1, gg_2, gg_3, gg_4,
                    labels = NULL,
                    ncol = 2, nrow = 4,
                    common.legend = TRUE, legend = "bottom",
                    align = "hv",
                    font.label = list(size = 10, color = "black", face = "bold", family = NULL, position = "top"))

pdf("figure.pdf")
figure
dev.off()
 
Run Code Online (Sandbox Code Playgroud)

ale*_*dra 15

对于使用 ggarrange 的公共轴,您可以尝试更改为:

require(grid)   # for the textGrob() function

figure <- ggarrange(gg_1 + rremove("ylab") + rremove("xlab"), gg_2 + rremove("ylab") + rremove("xlab"), gg_3 + rremove("ylab") + rremove("xlab"), gg_4+ rremove("ylab") + rremove("xlab"), # remove axis labels from plots
                    labels = NULL,
                    ncol = 2, nrow = 2,
                    common.legend = TRUE, legend = "bottom",
                    align = "hv", 
                    font.label = list(size = 10, color = "black", face = "bold", family = NULL, position = "top"))

annotate_figure(figure, left = textGrob("Common y-axis", rot = 90, vjust = 1, gp = gpar(cex = 1.3)),
                    bottom = textGrob("Common x-axis", gp = gpar(cex = 1.3)))
Run Code Online (Sandbox Code Playgroud)

gp = gpar(cex = 1.3)部分更改字体大小。公共图例之前有如此多空间的原因是因为您nrow = 4在 中指定了ggarrange,但最后 2 行未填充。图形列之间存在间隙的原因是因为您包含了theme(aspect.ratio = 1)每个图,因此要减少此间隙,您必须减小图形的宽度ggarrange,或者注释掉/删除纵横比位。