Skip to content

Aligning legends? #194

@courtiol

Description

@courtiol

Perhaps I am missing something, but is there a functionality to align legends that have been extracted from plots?
The silly example below illustrates the issue: while the plots are aligned, the legends are not.
I experimented with alignment options but failed to find a good solution.
I did check both relevant vignettes, but did not find a solution in there either.

library(ggplot2)
library(cowplot)

nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)

plot1 <- plot2 <- ggplot(nc[1:40, ]) +
  geom_sf(aes(fill = AREA)) +
  labs(fill = NULL)

plot3 <- plot4 <- ggplot(nc[1:40, ]) +
  geom_sf(aes(fill = substr(NAME, start = 1, stop = 1))) +
  labs(fill = NULL)

legend_top <- get_legend(plot1)
legend_bottom <- get_legend(plot3)

plot_grid(legend_top,
          plot1 + theme(legend.position = "none"),
          plot2 + theme(legend.position = "none"),
          legend_bottom,
          plot3 + theme(legend.position = "none"),
          plot4 + theme(legend.position = "none"),
          nrow = 2,
          rel_widths = c(0.15, 0.425, 0.425))

Thanks for looking into this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions