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

key height gets stretched when using multiline key-labels or large font size in the legend #3669

Open
mluerig opened this issue Dec 14, 2019 · 3 comments · May be fixed by #6279
Open

key height gets stretched when using multiline key-labels or large font size in the legend #3669

mluerig opened this issue Dec 14, 2019 · 3 comments · May be fixed by #6279
Labels
feature a feature request or enhancement guides 📏 visual change 👩‍🎨 Rendering change that will affect look of output

Comments

@mluerig
Copy link

mluerig commented Dec 14, 2019

see this issue: wilkelab/cowplot#152 (reprex included)

the key height gets stretched when using multiline key-labels OR bigger text size in the legend. this is not affected my manipulations of the key height in theme() or guides() - is this intended behavior?


The reprex from wilkelab/cowplot#152 (comment):

require(ggplot2)
#> Loading required package: ggplot2

set.seed(15)

df <- data.frame(x=1:50, y=rnorm(50, 10, 2), var=rep(c("A","B","C","D","E"),10))
labs = c("A oneline","B oneline","C oneline", "D\ntwolines","E\ntwolines")
cols = c('#e41a1c','#377eb8','#4daf4a','#984ea3','#ff7f00')


ggplot(df) + 
  geom_ribbon(aes(x=x, ymin=y-1, ymax=y+1, fill=var), alpha=0.3) +
  geom_line(aes(x=x, y=y, colour=var), alpha=0.3, size=2) +
  scale_fill_manual(values=cols, labels = labs) +
  theme(legend.position="bottom",
        legend.title = element_blank(),
        legend.text = element_text(size = 14)) +
  guides(fill=guide_legend(ncol=3, byrow=TRUE))

@thomasp85 thomasp85 added bug an unexpected problem or unintended behavior guides 📏 labels Jan 21, 2020
@gfspiteri
Copy link

The same happens if you increase margins around the text labels in the legend.

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

This is deliberate, though I can understand why it may be undesirable... We may consider adding a switch in the guide to control this...

@thomasp85 thomasp85 added feature a feature request or enhancement visual change 👩‍🎨 Rendering change that will affect look of output and removed bug an unexpected problem or unintended behavior labels Apr 20, 2021
@twest820
Copy link

+1 for switch in the guide. From what I've tried, the current behavior here is ggplot silently overrides legend.key.height or the y component legend.key.size if either theme requests a fill legend key whose height is shorter than the text but does honor these settings if you to make the key larger than the text is tall. Makes me wonder if the problem is maybe the key drawing doesn't know whether it's getting a default height that's ok to stretch or if it's blindly overriding a user specification.

There are some workarounds based on replacing ggplot's legend drawing in the Stackoverflow question on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement guides 📏 visual change 👩‍🎨 Rendering change that will affect look of output
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants