-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Independent justification of legends #6417
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
Comments
The development documentation reads under
So this is currently not possible. The ggproto object in charge is |
This is a pity. I'd like to mark it as a feature request then. In the meantime, if you can point me to how to extend the Guide proto to get this, I'd be grateful. In my final plot I'm already using a modified version of GuideLegend anyway (to use the same colors for the small country legend as in the main fill legend). |
There is some guidance on extending the The legend justification is a property of the guide-box, which is an ephemoral structure without associated class. There is no extending the guide-box. It is managed by the |
A workaround for this is to use inside guide legends, which support separate justifications and positions with the development version of ggplot2. However, you’ll need to adjust the plot margins accordingly: library(ggplot2)
rnaturalearth::ne_countries() |>
dplyr::mutate(value = sample(1:10, dplyr::n(), replace = TRUE)) |>
ggplot() +
geom_sf(
aes(fill = value, color = value, geometry = geometry)
) +
guides(
fill = guide_colorbar(
position = "inside",
theme = theme(
legend.justification.inside = c(1.5, 1),
legend.position.inside = c(0, 1)
)
),
color = guide_legend(
position = "inside",
theme = theme(
legend.justification.inside = c(1.5, 0),
legend.position.inside = c(0, 0)
)
)
) +
theme_minimal() +
theme(plot.margin = margin(l = 50)) |
It seems you're still using a version of ggplot2 that doesn't support multiple inside guide legends. Could you try reinstalling the development version of ggplot2 and test again? Also, would you mind sharing the output of |
ok, now it works thanks! |
That would depend on the core ggplot2 developers, but in my opinion, it's quite difficult to implement this feature. Since guide legends around the plot can only occupy a single position ( |
How would you propose the following would be resolved? E.g. we want What should happen? Does that change when we resize the plotting window so that enough space becomes available? Do we group legends first based on their justification values and then place them? library(ggplot2)
ggplot(mpg, aes(displ, hwy)) +
geom_point(aes(shape = factor(year), colour = drv, size = cty)) +
guides(
shape = guide_legend(theme = theme(legend.justification.right = c(0, 1))),
size = guide_legend(theme = theme(legend.justification.right = c(0, 1))),
colour = guide_legend(theme = theme(legend.justification.right = c(0, 0.5)))
) Created on 2025-04-14 with reprex v2.1.1 |
Yes, I do think that this feature is challenging to implement. However, I think the multiple legends arrangement should be a possible feature—similar to how If that works, we could arrange the Just like Legend 1 and Legend 3 (shape and size), Legend 2 (color) |
Thanks for the input Yunuuuu!
Can we give this separate consideration in a new issue? As for this current issue, perhaps we should allow |
Thanks, I'll open a new issue for the multiple legends arrangement as a feature request. |
Hello,
I'm trying to reproduce a map like the following:
but I cannot find a way to have two different legends both positioned on one side but one on top and one below:
The text was updated successfully, but these errors were encountered: