如何将多个图合并到一张图中?

Uma*_*HAN 0 r ggplot2

我需要一些帮助来将四个单独的图合并在一个页面上(单个图)。我使用相同的代码但使用不同的数据组绘制了四个不同的图。我需要在一页上绘制所有四个图。

library(ggpubr)
library(rstatix)
library(xlsx)
df<-read.xlsx("umar.xlsx", header = T, 1)
# Statistical test

######## Figure 1 ########
stat.test <- df %>%
  t_test(Moisture_content ~ substrate) %>%
  add_significance()
stat.test

# Box plots with p-values
bxp <- ggboxplot(df, x = "substrate", y = "Moisture_content", fill = "substrate", 
                 palette = c("#00AFBB", "#E7B800"), width = 0.35)



stat.test <- stat.test %>% add_xy_position(x = "substrate")
bxp + 
  stat_pvalue_manual(stat.test, label = "p = {p}") +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))+
  theme(
    aspect.ratio = 3
  )


######## Figure 2 ########
stat.test <- df %>%
  t_test(pH ~ substrate) %>%
  add_significance()
stat.test

# Box plots with p-values
bxp <- ggboxplot(df, x = "substrate", y = "pH", fill = "substrate", 
                 palette = c("#00AFBB", "#E7B800"), width = 0.35)



stat.test <- stat.test %>% add_xy_position(x = "substrate")
bxp + 
  stat_pvalue_manual(stat.test, label = "p = {p}") +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))+
  theme(
    aspect.ratio = 3
  )


######## Figure 3 ########
stat.test <- df %>%
  t_test(EC ~ substrate) %>%
  add_significance()
stat.test

# Box plots with p-values
bxp <- ggboxplot(df, x = "substrate", y = "EC", fill = "substrate", 
                 palette = c("#00AFBB", "#E7B800"), width = 0.35)



stat.test <- stat.test %>% add_xy_position(x = "substrate")
bxp + 
  stat_pvalue_manual(stat.test, label = "p = {p}") +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))+
  theme(
    aspect.ratio = 3
  )

######## Figure 4 ########
stat.test <- df %>%
  t_test(Moisture_content ~ substrate) %>%
  add_significance()
stat.test

# Box plots with p-values
bxp <- ggboxplot(df, x = "substrate", y = "Moisture_content", fill = "substrate", 
                 palette = c("#00AFBB", "#E7B800"), width = 0.35)



stat.test <- stat.test %>% add_xy_position(x = "substrate")
bxp + 
  stat_pvalue_manual(stat.test, label = "p = {p}") +
  scale_y_continuous(expand = expansion(mult = c(0.05, 0.1)))+
  theme(
    aspect.ratio = 3
  )
Run Code Online (Sandbox Code Playgroud)

小智 6

组合多个基于 ggplot 的绘图的最简单方法是使用patchwork包:

library(patchwork)
plot1 + plot2 + plot3 + plot4
Run Code Online (Sandbox Code Playgroud)


小智 5

使用 ggpubr 中的 ggarrange,如下所示:
library(ggplot2)

plot_1 <- your_ggplot_code
plot_2 <- your_ggplot_code

library(ggpubr)

#See ?ggarrange

combined_plot <- ggarrange(plot_1,
                           plot_2,
                           nrow = 2,
                           ncol = 1) #nrow & ncol depend on how you want to 
                                     #organize your plots
Run Code Online (Sandbox Code Playgroud)

祝你好运!