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

Length of legend title affects justification of legend keys/labels? #1903

Closed
orb16 opened this issue Nov 13, 2016 · 3 comments · Fixed by #5570
Closed

Length of legend title affects justification of legend keys/labels? #1903

orb16 opened this issue Nov 13, 2016 · 3 comments · Fixed by #5570
Labels
bug an unexpected problem or unintended behavior guides 📏 themes 💃

Comments

@orb16
Copy link

orb16 commented Nov 13, 2016

This produces a nicely centred title (and centred legend text):

require(ggplot2)
data(iris)
ggplot(iris, aes(y = Sepal.Width, x = Petal.Width)) + 
geom_point(shape = 21, aes(fill = Species)) + 
guides(fill = guide_legend(title.position = "top", title.hjust = 0.5))  + 
scale_fill_manual("Species", values = c("orange", "black", "white")) 

Here changing the length of the legend title appears to affect the justification of the legend text:

ggplot(iris, aes(y = Sepal.Width, x = Petal.Width)) + 
geom_point(shape = 21, aes(fill = Species)) + 
guides(fill = guide_legend(title.position = "top", title.hjust = 0.5))  + 
scale_fill_manual("A really, really long title that is sometimes necessary involving species", 
values = c("orange", "black", "white")) 

Using ggplot2_2.1.0 and R version 3.2.4 Revised

@hadley hadley added bug an unexpected problem or unintended behavior themes 💃 labels Jan 25, 2017
@hadley
Copy link
Member

hadley commented Nov 1, 2017

Minimal reprex:

library(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.box.just = "center")

I think the current behaviour is technically correct - the title is centered, but the legend box is not.

@clauswilke
Copy link
Member

This SO answer provides a way to center the legend box underneath the title:
https://stackoverflow.com/a/48011882/4975218
The code is general and could be integrated into the legend drawing code. I might tackle it, but not for 2.3.0.

One downside to this approach is that if there are multiple legends then they will be centered relative to each other but not overall. A better solution would fix the overall case as well.

@thomasp85 thomasp85 added this to the ggplot2 3.3.4 milestone Mar 25, 2021
@thomasp85
Copy link
Member

I think this reaches beyond the currently provided theme settings... The title alignment is basically doing what it should, and so does legend.box.just since it aligns multiple legends with each others (and will thus not have any effect on single legend plots)

We will need a new setting that takes care of internal alignment of titles and keys with respect to each other

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 📏 themes 💃
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants