thomasp85 / patchwork

The Composer of ggplots
https://patchwork.data-imaginist.com
Other
2.42k stars 157 forks source link

Error when using `theme(strip.placement = 'outside')` to create layouts #325

Closed TanyaS08 closed 11 months ago

TanyaS08 commented 1 year ago

Hi, thanks so much for the awesome package!

When using theme(strip.placement = 'outside') for plots its seems to be throwing errors when trying to patchwork more complex layouts, it throws the following error message Error in FUN(X[[i]], ...) : subscript out of bounds

As a minimum working example I'll create two of the same plots with the exception that p1 has a theme(strip.placement = 'outside') call

library(ggplot2)
library(patchwork)

p1 = ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point() +
  facet_grid(rows = vars(year),
             cols = vars(class),
             switch = 'y') +
  theme(strip.placement = 'outside')

p2 = ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point() +
  facet_grid(rows = vars(year),
             cols = vars(class),
             switch = 'y')

If I want to combine p1 + p2 I get the above error message. However p2 + p2 behaves as expected

I had initially discovered the bug when trying to use inset_element() where the base plot is the one with the theme(strip.placement = 'outside') call so in this case p1. Interestingly if the inset plot is the one with the additional outside call then it works? i.e. p2 + inset_element(p1, left = 0.6, bottom = 0.6, right = 1, top = 1) works as expected but p1 + inset_element(p2, left = 0.6, bottom = 0.6, right = 1, top = 1) throws the FUN(X[[i]], ...) : subscript out of bounds error

I am currently running ggplot2 v3.4.2 and patchwork v1.1.2