【问题标题】:Shared x and y axis labels ggplot2 with ggarrange与 ggarrange 共享 x 和 y 轴标签 ggplot2
【发布时间】:2021-02-21 17:06:39
【问题描述】:

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

我查看了以下主题: ggplot2 grid_arrange_shared_legend share axis labels

ggplot: align plots together and add common labels and legend

Add common axis titles with lines/arrows for multiple plots in ggplot

ggplot: how to add common x and y labels to a grid of plots

但我认为我想做的事情要简单一些,我不知道如何将 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()
 

【问题讨论】:

  • 尝试使用pdf("figure.pdf", width=7, height=14)。请记住,在您的情节中有 2 个空行(nrow = 2 将是 ggarrange 中的正确选择)。

标签: r ggplot2


【解决方案1】:

对于使用 ggarrange 的常用坐标轴,您可以尝试更改为:

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))

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多