ggarrange: combining multiple plots with shared y and x axis: 仅安排一个共享的 y 轴,同时保持相同比例

ggarrange: combining multiple plots with shared y and x axis: arrange for only one shared y axis while keeping plots in same proportion

我使用 ggpubr::ggarrange 创建了一个具有共享 y 轴和 x 轴的多图。我遇到的唯一问题是第一个图确实有 y 轴小于其他 3 个图,这使整个图不成比例。

因此,我正在寻找一种解决方案,只显示一个 y 实验室,而不会使第一个图与其他 3 个图不成比例。非常感谢您的帮助,因为我已经安静了一段时间,一直在寻找解决这个问题的方法。

到目前为止,我的方法是从图 (p) 2、3、4 中删除 y 实验室并将其保留在 p1 上。

这是我的代码:

library(ggplot2)
  library(ggpubr)
  library(dplyr)
p1 <-  ggplot(arrange(ploughed1, Horizont), aes(Ferment, RAI_II,  fill = factor(Horizont, levels=c("4","3","2","1"))))+
    geom_bar(stat = "identity", position = "dodge")+
    scale_fill_manual(values = c("#FF9933", "#CC6600","#663300","#000000"))+
    guides(fill = guide_legend(reverse = TRUE))+
    labs(fill="Horizon")+
    ylim(0,200)+
    theme_bw()+
    facet_wrap(~compost)+
    theme(strip.text = element_text(size = 7),
          panel.spacing = unit(0.2, "lines"))+
    geom_col(position = position_stack(reverse = TRUE))+
    labs(x="Ferment", y = "RAI_II=Rooting*Scheme*Active",  title = "P- ")

p2 <-  ggplot(arrange(ploughed2, Horizont), aes(Ferment, RAI_II,  fill = factor(Horizont, levels=c("4","3","2","1"))))+
    geom_bar(stat = "identity", position = "dodge")+
    scale_fill_manual(values = c("#FF9933", "#CC6600","#663300","#000000"))+
    guides(fill = guide_legend(reverse = TRUE))+
    labs(fill="Horizon")+
    ylim(0,200)+
    theme_bw()+
    theme(axis.text.y = element_blank(),
          panel.spacing = unit(0.2, "lines"),
          strip.text = element_text(size = 7))+
    facet_wrap(~compost)+
    geom_col(position = position_stack(reverse = TRUE))+
    labs(x="Ferment",  title = "P+ ")+ 
    rremove("ylab")

  p3 <-  ggplot(arrange(reduced1, Horizont), aes(Ferment, RAI_II,  fill = factor(Horizont, levels=c("4","3","2","1"))))+
    geom_bar(stat = "identity", position = "dodge")+
    scale_fill_manual(values = c("#FF9933", "#CC6600","#663300","#000000"))+
    guides(fill = guide_legend(reverse = TRUE))+
    labs(fill="Horizon")+
    ylim(0,200)+
    theme_bw()+
    theme(axis.text.y = element_blank(),
          panel.spacing = unit(0.2, "lines"),
          strip.text = element_text(size = 7))+
    facet_wrap(~compost)+
    geom_col(position = position_stack(reverse = TRUE))+
    labs(x="Ferment",  title = "RT- ")+ 
    rremove("ylab")
  
  p4 <-  ggplot(arrange(reduced2, Horizont), aes(Ferment, RAI_II,  fill = factor(Horizont, levels=c("4","3","2","1"))))+
    geom_bar(stat = "identity", position = "dodge")+
    scale_fill_manual(values = c("#FF9933", "#CC6600","#663300","#000000"))+
    guides(fill = guide_legend(reverse = TRUE))+
    labs(fill="Horizon")+
    ylim(0,200)+
    theme_bw()+
    theme(axis.text.y = element_blank(),
          panel.spacing = unit(0.2, "lines"),
          strip.text = element_text(size = 7))+
    facet_wrap(~compost)+
    geom_col(position = position_stack(reverse = TRUE))+
    labs(x="Ferment",  title = "RT+ ")+ 
    rremove("ylab")
ggarrange(p1, p2, p3, p4, nrow=1, common.legend = TRUE)

输出.png

输出图像:1

我也尝试在ggarrange函数中解决,不去掉p2,p3,p4中的y lab和text,结果一样。

ggarrange(p1, p2+ 
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank() ), p3+ 
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank() ), p4+ 
            theme(axis.text.y = element_blank(),
                  axis.ticks.y = element_blank(),
                  axis.title.y = element_blank() ) , nrow=1,  common.legend = TRUE)

如果您可以选择其他软件包,我建议您使用 patchwork。使用一些方便的函数来减少重复代码和一些随机示例数据来模拟您的真实数据:

library(ggplot2)
library(patchwork)
library(dplyr)

ploughed1 <- data.frame(
  Horizont = rep(1:4, 4),
  RAI_II = runif(16, 10, 50),
  Ferment = rep(c("-", "+"), each = 8),
  compost = rep(c("- Compost", "+ Compost"), each = 4)
)

plot_fun <- function(x, title) {
  ggplot(arrange(x, Horizont), aes(Ferment, RAI_II, fill = factor(Horizont, levels = c("4", "3", "2", "1")))) +
    geom_bar(stat = "identity", position = "dodge") +
    scale_fill_manual(values = c("#FF9933", "#CC6600", "#663300", "#000000")) +
    guides(fill = guide_legend(reverse = TRUE)) +
    ylim(0, 200) +
    theme_bw() +
    facet_wrap(~compost) +
    theme(
      strip.text = element_text(size = 7),
      panel.spacing = unit(0.2, "lines")
    ) +
    geom_col(position = position_stack(reverse = TRUE)) +
    labs(x = "Ferment", y = "RAI_II=Rooting*Scheme*Active", fill = "Horizon", title = title)
}

remove_y <- theme(
  axis.text.y = element_blank(),
  axis.ticks.y = element_blank(),
  axis.title.y = element_blank()
)
p <- list(
  plot_fun(ploughed1, "P-"),
  plot_fun(ploughed1, "P+") + remove_y,
  plot_fun(ploughed1, "RT-") + remove_y,
  plot_fun(ploughed1, "RT+") + remove_y
)
wrap_plots(p, nrow = 1) + plot_layout(guides = "collect")

patchwork 相比,在每个绘图中所有面都具有相同的宽度,由于 y 比例,使用 ggpubr:ggarrange 挤压第一个图中的面:

ggpubr::ggarrange(plotlist = p, nrow = 1, common.legend = TRUE)