This is about organizing plots in R markdown document. Say we create three plots:
library(patchwork)
library(tidyverse)
a <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + ggtitle("Plot A")
b <- ggplot(mtcars, aes(drat, wt)) + geom_point() + ggtitle("Plot B")
c <- ggplot(mtcars, aes(cyl, wt)) + geom_point() + ggtitle("Plot C")
My organization is:
Plot<- a+b+c +plot_layout(ncol = 2)
It’s OK. But what I really want is:
plot<- a /(b + c) +
plot_layout(ncol = 2)
Except that all three plots are the same size, the upper centered.
Something very similar can be done with plot_spacer():
plot<- plot_spacer() + a + plot_spacer() /(b + plot_spacer() + c)
But that decrease the size of the plots. Is there a way to keep the size of the plots, only center the upper one?
design <- "
#11#
2233"
wrap_elements(full = a) + b + c + plot_layout(design = design, widths = 1)
The wrap_elements
part is used to make the plots equal in width. (Though I haven't been able to get them to be the same height without manually adding heights = c(1.2,1)
)
As the patchwork
author notes here, "the issue is that the y-axis of the top plot influences the width of plots spanning it's location. The easiest way is to remove alignment from the top plot by putting it inside wrap_elements()."