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

Aligning legends? #194

Open
courtiol opened this issue Jun 27, 2023 · 0 comments
Open

Aligning legends? #194

courtiol opened this issue Jun 27, 2023 · 0 comments

Comments

@courtiol
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant