You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?!?
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:
Reacted by David Schoch, Lluís Revilla, Rubem Dornas, Gorka Navarrete, Mark, Michał Krassowski, Mike Jay, Desmond Choy, Tyson Barrett, quanquan and 67 more
Reacted by Antonio Alvarez, Beatriz Milz, Max Blasdel, Vilmar Benetti Filho, bersco, Thomas Bradley and Douglas Lawton
Reacted by B. Arman Aksoy, John-Henry Pezzuto, Beatriz Milz, Evan Muzzall, Lucas, Beka Stiling, Merritt Khaipho-Burch, Richard Abdill, Vilmar Benetti Filho, Daniel Malone and 9 more
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
Reacted by Eric R. Scott and Nicolás X. Gómez Andújar
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:
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?
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').
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
Activity
thomasp85 commentedon Dec 3, 2019
You just need to make sure that the theming of the patchwork has
legend.position = 'bottom'as well. This can be done using either thethemeargument inplot_annotation(), or by&a theme object to it which will apply the theme to both the patchwork as well as to all subplots:melissagwolf commentedon Apr 1, 2020
This helped so much. If you write the code as
+ themeinstead of& theme, the legend position will not go where it is specified. The&is key.MatthieuStigler commentedon Apr 28, 2020
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 commentedon May 10, 2020
is the manual position of the legend still not possible?
melissagwolf commentedon May 12, 2020
Hi Thomas,
It seems like
guides=collectmight 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 commentedon 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 commentedon May 12, 2020
Yep, using 4.0.0. Thank you for the quick response!
nahorp commentedon Mar 11, 2021
Thank. you. so. much. you are a godsend
costavale commentedon Nov 10, 2021
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 commentedon Jul 27, 2023
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 specifyingguide_area()but this did not work because the legend box was in always in vertical orientation (ignoringlegend.box='horizontal').OlivaresLD commentedon Feb 21, 2026
@costavale In my case, I found two options, one not very fancy, but working and also a downgrading option. So, you can
plot_annotation()like this:p1 + p2 + plot_layout(ncol = 2, guides = "collect") +plot_annotation(theme = theme_minimal() +theme(legend.position = "bottom"))or