Skip to content

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

Open
@almita

Description

@almita

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions