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

Collecting guides from list of plots with partially matching legends #410

Open
almita opened this issue Nov 16, 2024 · 2 comments
Open

Collecting guides from list of plots with partially matching legends #410

almita opened this issue Nov 16, 2024 · 2 comments

Comments

@almita
Copy link

almita commented Nov 16, 2024

Here's an example:

set.seed(10)
library(dplyr)
library(purrr)
library(ggplot2)
library(patchwork)
data <- list()
for(i in 1:4){
    x <- rnorm(4)
    y <- rnorm(4)
    z <- sample(x = c("B1", "B2", "B3", "B4"), replace = TRUE)
    data[[i]] <- data.frame(x,y,z)
}

pal <- c("B1"="red", "B2"="blue", "B3"="green", "B4"="black")

names(data) <- c("A1", "A2", "A3", "A4")

plots <- map(data, ~ .x %>% ggplot(aes(x, y, colour = z))+
    geom_point() +
        scale_color_manual(values = pal)) 

wrap_plots(plots, guides = "collect")

image

So what I'd like is to be able to have just one legend with B1, B2, B3, and B4. Is that possible?

@trekonom
Copy link

guides="collect" will only collect legends when they are identical, which for your case can be achieved by setting the limits= to include all categories. Additionally in ggplot2 >= 3.5.0 one also has to add show.legend=TRUE to the geom display a legend key for unused levels:

set.seed(10)
library(dplyr, warn = FALSE)
library(purrr)
library(ggplot2)
library(patchwork)

data <- list()
for (i in 1:4) {
  x <- rnorm(4)
  y <- rnorm(4)
  z <- sample(x = c("B1", "B2", "B3", "B4"), replace = TRUE)
  data[[i]] <- data.frame(x, y, z)
}

pal <- c("B1" = "red", "B2" = "blue", "B3" = "green", "B4" = "black")

names(data) <- c("A1", "A2", "A3", "A4")

plots <- map(data, ~ .x %>% ggplot(aes(x, y, colour = z)) +
  geom_point(show.legend = TRUE) +
  scale_color_manual(values = pal, limits = names(pal)))

wrap_plots(plots, guides = "collect")

@almita
Copy link
Author

almita commented Nov 16, 2024

Yeah I figured they had to be identical, thanks for your solution!! This is very helpful

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

2 participants