Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
schochastics opened this issue Dec 3, 2019 · 10 comments
Closed

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

schochastics opened this issue Dec 3, 2019 · 10 comments

Comments

@schochastics
Copy link

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

@thomasp85
Copy link
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')

@melissagwolf
Copy link

melissagwolf commented Apr 1, 2020

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.

@MatthieuStigler
Copy link

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!

@costavale
Copy link

is the manual position of the legend still not possible?

@melissagwolf
Copy link

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

@llrs
Copy link

llrs commented May 12, 2020

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

@melissagwolf
Copy link

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

@nahorp
Copy link

nahorp commented Mar 11, 2021

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

@costavale
Copy link

costavale commented Nov 10, 2021

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?

@krassowski
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants