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

Long legend title justification #5570

Merged
merged 6 commits into from
Dec 11, 2023

Conversation

teunbrand
Copy link
Collaborator

@teunbrand teunbrand commented Dec 7, 2023

This PR aims to fix #1903.

To recap, when it comes to legend* title justification there are two situations to keep in mind:

  1. The legend title is smaller than the legend.
  2. The legend title is larger than the legend.

* also applies to colourbars, coloursteps and bin legend

Currently, the title's justification applies only in situation 1. In other words, title justification is currently meaningless in situation 2.

This PR extends meaning to title justification in situation 2, by aligning the keys/labels with the title. Previously, these were hardcoded to be left-aligned with oneanother (or top-aligned if title position was left/right). The title justification settings now have the following simplified meaning:

  • hjust = 0 the legend title and keys should be left-aligned.
  • hjust = 1 the legend title and keys should be right-aligned.
  • vjust = 0 the legend title and keys should be bottom-aligned.
  • vjust = 1 the legend title and keys should be top-aligned.

The approach is the same as demonstrated by Claus in #1903 (comment), but integrated into the legend code. I decided against Thomas' comment to separate this into yet another theme setting, because situation 2 had no meaning and could be given meaning.

To demonstrate, the reprex from #1903 can now right-align the title with the keys/labels:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
df <- data.frame(x = c("a", "bbbbbbbbbbbbb"))

ggplot(df, aes(x = x, fill = x)) +
  geom_bar() +
  labs(fill = "A very very very long label") +
  theme(legend.title = element_text(hjust = 1))

Created on 2023-12-07 with reprex v2.0.2

@teunbrand teunbrand added this to the ggplot2 3.5.0 milestone Dec 7, 2023
@teunbrand teunbrand changed the title Legend title justification Long legend title justification Dec 7, 2023
@teunbrand teunbrand requested a review from thomasp85 December 7, 2023 15:27
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
Copy link
Collaborator Author

@thomasp85 I've applied the same logic to guide_custom() too now which I forgot about. I don't know if this should be reviewed again.

@thomasp85
Copy link
Member

LGTM

@teunbrand
Copy link
Collaborator Author

Thanks Thomas!

@teunbrand teunbrand merged commit 80db793 into tidyverse:main Dec 11, 2023
12 checks passed
@teunbrand teunbrand deleted the legend_title_justification branch December 11, 2023 13:50
@teunbrand teunbrand mentioned this pull request Dec 11, 2023
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.

Length of legend title affects justification of legend keys/labels?
2 participants