Skip to content

Fix legend key sizes #6485

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

Merged
merged 2 commits into from
Jun 3, 2025
Merged

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented May 29, 2025

This PR aims to fix #6484

Briefly, in #6340 we override user input in colour bars and descendants. We should've edited it, not override it.
Reprex from issue

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

p <- ggplot(mpg) +
  geom_point(aes(x = factor(cyl), y = displ, color = hwy)) +
  scale_color_fermenter(palette = "RdYlBu", direction = 1) +
  theme(legend.key.width = unit(0.25, "lines"))

plots <- list(
  p + ggtitle("default"),
  p + ggtitle("0.5 lines") + theme(legend.key.height = unit(0.5, "lines")),
  p + ggtitle("2.5 lines") + theme(legend.key.height = unit(2.5, "lines")),
  p + ggtitle("null") + theme(legend.key.height = unit(1, "null"))
)

cowplot::plot_grid(plotlist = plots)

Created on 2025-05-29 with reprex v2.1.1

@teunbrand teunbrand added this to the ggplot2 4.0.0 milestone May 29, 2025
@Yunuuuu
Copy link
Contributor

Yunuuuu commented May 29, 2025

In this line, we used the provided spacing which suggests that the input spacing should be preserved and not modified.

relative_sum <- sum(relative) + num_null(spacing)

But this line

spacing <- sum(rep(spacing, length.out = length(units) - 1))
it seems the spacing is being changed. Is this intentional?

@teunbrand
Copy link
Collaborator Author

Yeah we need to repeat the spacing for in between every guide. So for 1 guide, we need no spacing, for 2 guides we need 1x spacing, for 3 guides 2x etc.

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@teunbrand teunbrand merged commit 16c5029 into tidyverse:main Jun 3, 2025
13 checks passed
@teunbrand teunbrand deleted the fix_legend_key_sizes branch June 3, 2025 10:36
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

Successfully merging this pull request may close these issues.

legend.key.height / width don't work in dev version
3 participants