wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
701 stars 84 forks source link

Aligning legends? #194

Open courtiol opened 1 year ago

courtiol commented 1 year ago

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.