Search code examples
rggplot2patchwork

how to create common / shared / central y-axis title for combined graphs with multiple panels?


Using ggplot, I have produced three graphs that I want to combine in a packed manner (on top of each other) using the package patchworks.

This is easily achieved by typing:

graph1/graph2/graph3

However, the problem that I run into is that for these three graphs I have a common y-axis, which I naturally want to have placed at the central graph (and I have therefore only created a y axis label for graph 2 when I made the graphs in ggplot). This y-axis label text is rather long. Therefore, I would like to extend it "beyond the boundaries" of the other graphs. However, my outcome is as shown on the picture below.

enter image description here

It can be seend that the y-axis label is allowed to extend into the top graph but it is not allowed to extend into the bottom graph (here the excess label text is simply removed as if the bottom graph is placed on top of the text).

How can I allow my y-axis label to also extend into the bottom graph? It is not an option to simply change the dimension of the picture afterwards so that the central graph is large enough to accommodate the label (then the graph would be too large to fit on one page).


Solution

  • This is currently still a long requested feature for patchwork (see this discussion). There are a couple of workarounds, below what I would do. I'd stitch the lab title as a plot to the others.

    library(ggplot2)
    library(patchwork)
    ls_p <- rep(list(ggplot(mtcars, aes(mpg, disp)) +geom_point()), 3)
    glob_lab <- "This long label which is too small for one plot"
    
    p_lab <- 
      ggplot() + 
      annotate(geom = "text", x = 1, y = 1, label = glob_lab, angle = 90) +
      coord_cartesian(clip = "off")+
      theme_void()
    
    (p_lab | wrap_plots(ls_p, nrow = 3)) +
      plot_layout(widths = c(.1, 1))
    

    Created on 2021-03-24 by the reprex package (v1.0.0)