rggplot2plotggpubr

How to combine multiple plots in R?


I want to arrange the plot in a specific order.

library(ggpubr)
data("ToothGrowth")
data("mtcars")
mtcars$name <- rownames(mtcars)
mtcars$cyl <- as.factor(mtcars$cyl)

# create boxplot
bxp <- ggboxplot(ToothGrowth, x = "dose", y = "len", color = "dose", palette = "jco")
# create dotplot
dp <- ggdotplot(ToothGrowth, x = "dose", y = "len", color = "dose", palette = "jco", binwidth = 1)

# create scatterplot
sp = ggscatter(mtcars, x = "wt", y = "mpg", add = "reg.line", conf.int = TRUE, color = "cyl", palette = "jco", shape = "cyl")+
  stat_cor(aes(color = cyl), label.x = 3)
# arrange the plots
ggarrange(sp,                                                 # First row with scatter plot
          ggarrange(bxp, dp, ncol = 2, labels = c("B", "C")), # Second row with box and dot plots
          nrow = 2, 
          labels = "A")                                       # Labels of the scatter plot

Arranging the plots this way has given me the output.

outputw

But, I want the output in another way like scatterplot should be in the down pane and bx and bp to be in top pane. I tried

ggarrange(bxp, dp, ggarrange(sp, labels = c("C"), nrow = 1),
         nrow = 2, ncol = 2, labels=c("A", "B"))

But it gave me like as follows:

output2

The scatterplot is not filled with down pane. It's in a corner of the pane


Solution

  • Try this.

    ggarrange(ggarrange(bxp, dp, 
                        nrow = 1, ncol = 2, labels=c("A", "B")),
              ggarrange(sp, labels = c("C"), nrow = 1, ncol = 1),
              nrow = 2, ncol = 1)
    

    enter image description here