Search code examples
rggplot2legendpatchwork

Why does my legend not move to empty area with patchwork using guide_area()


I have a plot 1 which covers the whole first row and a plot 2 which is a bit smaller below. They have a common guide and I arranged them using patchwork.
Now I want to move the guide to the right of plot_2 as there is a blank space, rather than to the right of both blots. I tis very similar to this problem: Manually position legend in Patchwork but it won't work. You can see it already tried incorporating guide_area(). Can someone explain me why my approach is not working? And how I can move the legend where I want it?

plot_1/(plot_2+guide_area()+plot_layout(widths = c(1.3, 1)))+
  plot_layout(guides = 'collect')+
  plot_annotation(title = 'title',
                  subtitle = 'subtitle',
                  tag_levels = 'A',tag_suffix = ')',
                  theme=theme(plot.title = element_text(size = 20,face='bold'),
                              plot.subtitle = element_text(size = 20))) &
  theme(plot.tag = element_text(size = 15,face='bold'))

enter image description here


Solution

  • I think the problem is that in your patchwork, the second row plots are nested within the overall patchwork, burying the guide_area. If you bring all plots to the same "level", it works. I usually solve the problem like so:

    design <- "aa \n bc"
    
    plot_1 + plot_2 + guide_area() + 
      plot_layout(
        design = design, 
        guides = "collect", 
        widths = c(1.3, 1)
      )