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

legend.justification not preserved when space is limited #3635

Closed
melissakey opened this issue Nov 19, 2019 · 0 comments · Fixed by #4437
Closed

legend.justification not preserved when space is limited #3635

melissakey opened this issue Nov 19, 2019 · 0 comments · Fixed by #4437
Labels
bug an unexpected problem or unintended behavior guides 📏
Milestone

Comments

@melissakey
Copy link

Issue:

When the legend is wider than the plot, the legend.justification option in theme is ignored. Here's a quick example where legend.justification is left, but the legend is too long, and extends past the plot in both directions (getting cutoff).

data(iris)
library(ggplot2)
library(magrittr)

p1 <- iris %>%
  ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Length, shape = Species, fill = Petal.Width), shape = 21) + 
  theme(
    axis.title.y = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    legend.position = "bottom",
    legend.justification = "left",
    legend.direction = "horizontal",
    legend.box = "horizontal",
    # legend.background = element_blank(),
    # legend.margin = margin(3, .5, .5, .5, "lines"),
    # legend.key.height = unit(1, "lines")
  ) + 
  geom_point()
p1

Created on 2019-11-19 by the reprex package (v0.3.0)

Backstory

In a single plot, having cut-off legends are problematic no matter which direction they go. When combining plots using patchwork (https://github.com/thomasp85/patchwork), changing the justification allows long legends to extend into the space of adjoining plots. For example, this allows guides to be manually combined across plots (apparently guide collection is now implemented in patchwork, but it wasn't present earlier.) It's also useful when the legend spacing differs from the plot spacing (small plot, large legend mixed with a larger plot with a smaller legend) - a situation I run into when use ggplot2 + patchwork to create heavily annotated heat maps.

@thomasp85 thomasp85 added bug an unexpected problem or unintended behavior guides 📏 labels Jan 21, 2020
@thomasp85 thomasp85 added this to the ggplot2 3.3.4 milestone Mar 25, 2021
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

Successfully merging a pull request may close this issue.

2 participants