R: cowplot plot_grid centering single figure on one row

632 Views Asked by At

With three individual plots plot1, plot2 and plot3, the command plot_grid(plot1, plot2, plot3, ncol = 2) creates a 2 x 2 plot area, in which the top row consists of two plots and the bottom row of a single plot aligned to the left. Hence, the bottom right corner of the plot area is empty.

How can I center the individual plot3 on the bottom row so that the 2 x 2 does not appear to be missing a fourth plot? Also, I would need plot3 to have the same size as the other two plots.

Reproducible example

library(ggplot2)
library(cowplot)

p1 <- ggplot(mtcars, aes(disp, mpg)) + 
    geom_point()
p2 <- ggplot(mtcars, aes(disp, mpg)) + 
    geom_point()
p3 <- ggplot(mtcars, aes(disp, mpg)) + 
    geom_point()
plot_grid(p1, p2, p3, ncol = 2) # creates 2 x 2 plot area with missing empty bottom right corner (instead of p3 centered on bottom row)
2

There are 2 best solutions below

0
Maël On BEST ANSWER

With nested cowplots:

library(cowplot)
library(ggplot2)
plot_grid(
  plot_grid(p1, p2, nrow = 1, ncol = 2),
  plot_grid(NULL, p3, NULL, nrow = 1, rel_widths = c(0.5, 1, 0.5)),
  nrow = 2
)

enter image description here

0
stefan On

One option to achieve your desired result would be to switch to patchwork which via the design argument of wrap_plots or plot_layout gives a lot of control to place the plots in a grid:

library(ggplot2)
library(patchwork)

p1 <- p2 <- p3 <- ggplot(mtcars, aes(disp, mpg)) + 
  geom_point()

design <- "
AABB
#CC#
"

list(p1, p2, p3) |> 
  wrap_plots(design = design)

enter image description here