2

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).

tjebo
  • 21,977
  • 7
  • 58
  • 94

1 Answers1

9

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)

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • Nice works well. I was initially in doubt about how to make a text string with a super script in. It seemed like the method from ggplot: glob_lab<-bquote('Periodic annual volume production ' ~ (m^3~ha^-1~year^-1)), just resulted in "Periodic annual volume production " ~ (m^3 ~ ha^-1 ~ year^-1) But then I could see that when I actually plot it, it is displayed in the correct way as I want. Just wanted to add this as clarification since other people might also be confused. – Ditlev Reventlow Mar 24 '21 at 13:54
  • How do you change the fontface of this glob_lab object? – Ditlev Reventlow Mar 24 '21 at 13:55
  • 1
    @DitlevReventlow this is a geom_text layer, so you can change it like you usually change the font face with geom_text... e.g., https://stackoverflow.com/questions/28487364/legend-for-geom-text-with-variable-font-family (just a super quick google, this question seems more specific, but also shows some code) – tjebo Mar 24 '21 at 22:46