0

I need some help to combine four individual plots on a single page (single plot). I draw four different plots using the same codes but with different data groups. I need to draw all four plots on a single page.

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
  )
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Umar KHAN
  • 45
  • 1
  • 1
  • 7
  • 1
    With `ggpubr::ggarrange`? – Vincent Guillemot Aug 25 '21 at 15:24
  • Hi @Vincent Guillemot thanks for the comments. Could you please provide some more details? Best regards, – Umar KHAN Aug 25 '21 at 15:26
  • 2
    Without your data, it's difficult to help. There are two options -- you can make different plots and then arrange them on a page using the code in the `ggpubr` or `cowplot` libraries as @VincentGuillemot suggests, or you can rehape your data to long format and use `facet_wrap()` or `facet_grid()` to plot the different outcomes in a single plot. – mikebader Aug 25 '21 at 15:27
  • Here is my data https://docs.google.com/spreadsheets/d/1r1SeqBTWwve1MBqKHg7Kg1h6ey09DVP4/edit?usp=sharing&ouid=102539758905103985682&rtpof=true&sd=true – Umar KHAN Aug 25 '21 at 15:33
  • 3
    Does this answer your question? [Side-by-side plots with ggplot2](https://stackoverflow.com/questions/1249548/side-by-side-plots-with-ggplot2) – germcd Aug 25 '21 at 15:41

2 Answers2

5

The easiest way to combine multiple ggplot-based plots is with the patchwork package:

library(patchwork)
plot1 + plot2 + plot3 + plot4
Brenton M. Wiernik
  • 1,006
  • 4
  • 8
  • Great rec, thanks! I was previously using `cowplot::plot_grid()` and couldn't for the life of me figure out why it was stretching my x-axis labels. This worked great! – nathan liang Jan 15 '23 at 07:57
4

Using ggarrange from ggpubr, like this:
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

Good luck!