Search code examples
rfunctionggplot2plottidyverse

Add a common ylab to patchwork plots in r


I can create a common_ylab for the plots produced by patchwork::wrap_plots().

But I wonder how to add that common_ylab to those plots (tried: plots + common_ylab)?

Note: Suppose we only have access to the plots object and can't manipulate the individual p objects.

library(ggplot2)
library(patchwork)
library(grid)

p1 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")
p2 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")
p3 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")
p4 <- ggplot(mtcars, aes(mpg,vs))+ geom_point()+ylab("")


(plots = wrap_plots(p1,p2,p3,p4))

# Common ylab:
(common_ylab = grid::grid.draw(grid::textGrob("VS", x = 0.02, rot = 90)))

# Tried the following without success:
plots + common_ylab

Solution

  • I would tend to add a dummy ggplot here:

    p5 <- ggplot() + geom_text(aes(1, 1, label = "vs"), angle = -90) + theme_void()
    
    (plots | p5) + plot_layout(widths = c(12, 1))
    

    enter image description here