Skip to content

legend.justification not preserved when space is limited #3635

Closed
@melissakey

Description

@melissakey

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorguides 📏

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions