Search code examples
rggplot2patchwork

Patchwork does not collect guides


Patchwork does not collect guides with code in documentation... the reprex code is following:

Does anyone know what is causing the issue here, please?

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.0.5
library(patchwork)
p1a <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp, colour = mpg, size = wt)) + 
  ggtitle('Plot 1a')

p2 <- ggplot(mtcars) + 
  geom_boxplot(aes(gear, disp, group = gear)) + 
  ggtitle('Plot 2')

p3 <- ggplot(mtcars) + 
  geom_point(aes(hp, wt, colour = mpg)) + 
  ggtitle('Plot 3')
p1a | (p2 / p3) + plot_layout(guides = 'collect')

Created on 2022-11-26 by the reprex package (v2.0.1)

Session info - patchwork is updated


Solution

  • (p1a | (p2 / p3)) + plot_layout(guides = 'collect') 
    

    enter image description here

    Without the parentheses, the plot_layout() will only relate to the immediately preceding term, (p2 / p3). From the documentation for plot_layout():

    If you are nesting grids, the layout is scoped to the current nesting level.

    I think in this case "the current nesting level" will be the most recent term.