Skip to content

collect guides when legend.position="bottom" #136

Description

@schochastics

I'd like to center a common legend below two plots (see reprex below).
This does not seem to work atm (at least not how I thought it could work), and I somehow expect/fear that this is technically not possible?!?

library(tidyverse)
library(patchwork)

set.seed(17)
dat1 <- tibble(x = rep(1:5,2),
              y = sample(1:10,10,replace = T),
              grp = rep(c("A","B"),each=5))

dat2 <- tibble(x = rep(1:5,2),
              y = sample(1:10,10,replace = T),
              grp = rep(c("A","B"),each=5))


p1 <- ggplot(dat1,aes(x,y)) + geom_line(aes(col=grp))
p2 <- ggplot(dat2,aes(x,y)) + geom_line(aes(col=grp))

p1 + p2 + plot_layout(guides = "collect")

p3 <- ggplot(dat1,aes(x,y)) + 
   geom_line(aes(col=grp)) + 
   theme(legend.position = "bottom")

p4 <- ggplot(dat2,aes(x,y)) + 
   geom_line(aes(col=grp)) + 
   theme(legend.position = "bottom")

p3 + p4 + plot_layout(guides = "collect")

Activity

  1. thomasp85 commented on Dec 3, 2019

    @thomasp85
    Owner

    You just need to make sure that the theming of the patchwork has legend.position = 'bottom' as well. This can be done using either the theme argument in plot_annotation(), or by & a theme object to it which will apply the theme to both the patchwork as well as to all subplots:

    p1 + p2 + plot_layout(guides = "collect") & theme(legend.position = 'bottom')
  2. melissagwolf commented on Apr 1, 2020

    @melissagwolf

    This helped so much. If you write the code as + theme instead of & theme, the legend position will not go where it is specified. The & is key.

  3. MatthieuStigler commented on Apr 28, 2020

    @MatthieuStigler

    Awesome! Might be worth emphasizing this point in the (great) online manual on plot_layout. As a newcomer, I came into the exact same issue.

    Thanks!

  4. costavale commented on May 10, 2020

    @costavale

    is the manual position of the legend still not possible?

  5. melissagwolf commented on May 12, 2020

    @melissagwolf

    Hi Thomas,

    It seems like guides=collect might not be working anymore? I'm re-running the reprex from the first post and I'm getting the same legend twice. However, the position is still working.

    Thanks!

    Melissa

  6. llrs commented on May 12, 2020

    @llrs

    Hi Melissa @melissagwolf,

    Are you using R 4.0.0 ? This is a know bug #170 (Unless your session Information says something different)

    Lluís

  7. melissagwolf commented on May 12, 2020

    @melissagwolf

    Yep, using 4.0.0. Thank you for the quick response!

  8. nahorp commented on Mar 11, 2021

    @nahorp

    This helped so much. If you write the code as + theme instead of & theme, the legend position will not go where it is specified. The & is key.

    Thank. you. so. much. you are a godsend

  9. costavale commented on Nov 10, 2021

    @costavale

    You just need to make sure that the theming of the patchwork has legend.position = 'bottom' as well. This can be done using either the theme argument in plot_annotation(), or by & a theme object to it which will apply the theme to both the patchwork as well as to all subplots:

    p1 + p2 + plot_layout(guides = "collect") & theme(legend.position = 'bottom')

    What if I don't want to apply the theme to all the subplots?
    In other words, I want to have the "collected" legend at the centre bottom of the first two plots, but not for the third one in the first line:
    (((p1|p2) + plot_layout(guides = "collect") & theme(legend.position = "bottom"))|p2 + plot_layout(guides = "keep"))/(p1|p2)

    I cannot manage to have this working. any solution?

  10. krassowski commented on Jul 27, 2023

    @krassowski

    I cannot manage to have this working. any solution?

    yup, this is pretty bad when including ggraph or circular visualisation which greatly benefit from theme_void() while other panels are using non-void themes. One thing I tried was manually specifying guide_area() but this did not work because the legend box was in always in vertical orientation (ignoring legend.box='horizontal').

  11. OlivaresLD commented on Feb 21, 2026

    @OlivaresLD

    What if I don't want to apply the theme to all the subplots? In other words, I want to have the "collected" legend at the centre bottom of the first two plots, but not for the third one in the first line: (((p1|p2) + plot_layout(guides = "collect") & theme(legend.position = "bottom"))|p2 + plot_layout(guides = "keep"))/(p1|p2)

    I cannot manage to have this working. any solution?

    @costavale In my case, I found two options, one not very fancy, but working and also a downgrading option. So, you can

    1. use plot_annotation() like this:

    p1 + p2 + plot_layout(ncol = 2, guides = "collect") +
    plot_annotation(theme = theme_minimal() +
    theme(legend.position = "bottom"))

    or

    1. downgrade ggplot2 from 4.x.x to 3.5.2 and run again, this seems to fix the conflict with patchwork
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions