How to center graphs in a plot in R?

Solution for How to center graphs in a plot in R?
is Given Below:

I´ve the following figure

enter image description here

and I´d like to have the two graphs of the third line centered. I´ve trying to get it using packages like cowplotand grid_arrange from ggplot2 but with no success because they change their width to fit with the width of the other two rows (like in this example,

enter image description here

Is it possible to have the last two graphs centered without changing their width?


Not the most elegant code, but it works. AFAIK there is a package where you can specify for each plot the size and location, but I haven’t found it. I only used cowplot and function plot_grid

Generating plot

x <- rnorm(100)
y <- x * 2 + rnorm(100)
plt.df <- data.frame(x, y)

p     <- ggplot(plt.df, aes(x = x, y = y)) + geom_point()

Arranging them in grids

  cowplot::plot_grid(p, p, p, p, p, p, ncol = 3, nrow = 2),
  cowplot::plot_grid(NULL, p, p, NULL, rel_widths = c(0.5, 1, 1, 0.5), nrow = 1),
  nrow = 2,
  rel_heights = c(2, 1)

enter image description here

I first define a grid of 6 plots as usual. I then define a grid of 4 plots, two of them are null, and use the rel_widths parameter to determine their size.
To combine all of them I again invoke plot_grid, which accepts the two previous grids. Now define the rel_heights parameter to make sure they are all similar in size (since there are two grids, one with twice more plots than the other, then the ratio should be 2).