Skip to content

Conversation

Yunuuuu
Copy link
Contributor

@Yunuuuu Yunuuuu commented May 29, 2025

fix #6487

@Yunuuuu
Copy link
Contributor Author

Yunuuuu commented May 29, 2025

  library(ggplot2)
  library(patchwork)
  p1 <- ggplot(mtcars) +
    geom_point(aes(mpg, disp)) +
    ggtitle("Plot 1")

  p3 <- ggplot(mtcars) +
    geom_point(aes(hp, wt, colour = mpg)) +
    ggtitle("Plot 3")
  p <- patchwork::wrap_plots(
    p1, p3 + scale_color_continuous(guide = guide_colorbar(
      theme = theme(legend.key.height = unit(1, "null"))
    )),
    ncol = 1L,
    guides = "collect"
  )
  print(p)

image

@teunbrand
Copy link
Collaborator

teunbrand commented May 30, 2025

Thank you for the PR! I like that this PR doesn't complicate the logic, however, I don't think it preserves some behaviour I care about (but haven't bothered to embody in a test yet).

In this example, the spacing between the legend should be around the same height as the guide_legend() (because legend has 3 keys, each of 1null height and we're using 3null spacing).
The following example is rendered using this PR's strategy:

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

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point(aes(colour = cty, fill = hwy, shape = drv)) +
  guides(fill = "legend")

p + theme(legend.spacing.y = unit(3, "null"), legend.key.height = unit(1, "null"))

But it should look like this:

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

@teunbrand teunbrand linked an issue May 30, 2025 that may be closed by this pull request
@Yunuuuu Yunuuuu closed this Jun 3, 2025
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.

null unit legend key size failed legend.key.height / width don't work in dev version

2 participants