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

Issue with guide_custom() messing up alignment of other guides #5727

Closed
kylebutts opened this issue Feb 28, 2024 · 5 comments
Closed

Issue with guide_custom() messing up alignment of other guides #5727

kylebutts opened this issue Feb 28, 2024 · 5 comments
Labels
bug an unexpected problem or unintended behavior guides 📏

Comments

@kylebutts
Copy link

I'm not sure if this is a bug with ggplot2 or with gridtext, but I want to add a guide_custom with a custom grob via gridtext. This messes up the alignment of the color guide. This doesn't happen with grid::textGrob. gridtext::textbox_grob returns a gTree, so maybe that's causing the problem. If this is an issue with gridtext, feel free to close!

Reprex:

library(ggplot2)
library(gridtext)
library(palmerpenguins)
data(penguins) 

ggplot(
  penguins, aes(x = body_mass_g, y = flipper_length_mm, color = species)
) + 
  geom_point() +
  guides(
    custom = guide_custom(
      grob = grid::textGrob("Flipper Length (mm)", just = "right")
    )
  ) +
  theme(
    legend.box = "vertical",
    legend.location = "plot",
    legend.position = "top",
    legend.justification = c(0, 1),
  ) 
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

ggplot(
  penguins, aes(x = body_mass_g, y = flipper_length_mm, color = species)
) + 
  geom_point() +
  guides(
    custom = guide_custom(
      grob = gridtext::textbox_grob(
        "Flipper Length (mm)", hjust = 0
      )
    )
  ) +
  theme(
    legend.box = "vertical",
    legend.location = "plot",
    legend.position = "top",
    legend.justification = c(0, 1),
  ) 
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-02-28 with reprex v2.1.0

@teunbrand
Copy link
Collaborator

I think textbox grobs from {ggtext} don't self-report the width of the text, making placement somewhat more annoying.
You can use legend.box.just = "left" to make it work anyhow. Below I added a border to the legend background so you can see the alignment better.

library(ggplot2)
library(palmerpenguins)

ggplot(
  penguins, aes(x = body_mass_g, y = flipper_length_mm, color = species)
) + 
  geom_point() +
  guides(
    custom = guide_custom(
      grob = gridtext::textbox_grob(
        "Flipper Length (mm)", hjust = 0
      )
    )
  ) +
  theme(
    legend.box = "vertical",
    legend.box.just = "left",
    legend.location = "plot",
    legend.position = "top",
    legend.justification = c(0, 1),
    legend.background = element_rect(colour = "black")
  ) 
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-02-28 with reprex v2.1.0

@clauswilke
Copy link
Member

If it's a feature that is missing in gridtext please file an issue there. A PR with a fix would be even better.

https://github.com/wilkelab/gridtext/issues

@teunbrand
Copy link
Collaborator

Now I'm starting to doubt guide_custom() again as it seems to have the wrong box for the regular text grob.
I'll try to dig a bit deeper in the coming days.

library(ggplot2)
library(palmerpenguins)

ggplot(
  penguins, aes(x = body_mass_g, y = flipper_length_mm, color = species)
) + 
  geom_point() +
  guides(
    custom = guide_custom(
      grob = grid::textGrob("Flipper Length (mm)", just = "right")
    )
  ) +
  theme(
    legend.box = "vertical",
    legend.location = "plot",
    legend.position = "top",
    legend.justification = c(0, 1),
    legend.background = element_rect(colour = "black")
  ) 
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_point()`).

Created on 2024-02-28 with reprex v2.1.0

@teunbrand teunbrand added guides 📏 bug an unexpected problem or unintended behavior labels Mar 1, 2024
@teunbrand
Copy link
Collaborator

teunbrand commented Mar 4, 2024

I've done a bit of investigating and this is going on.

For the textGrob() thing: the text is right-aligned to the center point of the area. So when using just = "right", you should simultaneously set x = unit(1, "npc") to properly right-align it. This is precisely the 'point-based' versus 'width/height-based' thing the internal titleGrob() function resolves for ggplot2.

For the textbox_grob() thing: it is doing the correct thing, except the default width = unit(1, "npc") should be set to NULL to use the actual width of the text. I can imagine the default is useful in some cases, so nothing is wrong with {gridtext}.

The legend.box.just setting is useful to set in this case anyway. So while yes, there are some confusing elements with guide_custom(), these mostly relate to grid-level details over which ggplot2 has little control. As everything appears to be in working order, I'm going to consider this issue resolved.

@kylebutts
Copy link
Author

Thank you @teunbrand! This is all super helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior guides 📏
Projects
None yet
Development

No branches or pull requests

3 participants