Skip to content

plot_layout(guides = "collect") no longer working #455

Description

@neellab-umd

With ggplot 3.5.1, patchwork 1.3.0 and R4.4.2, the guides from a histogram and a density plot as specified below were merged into one as long as the color was removed from the density plot. I have just reloaded that configuration and tested that the guides are correctly collected into one.

With ggplot 4.0, patchwork 1.3.2 and the dev version 1.3.2.9 in R4.5.1 this no longer works. No matter what I do I get two legends as shown below. I can't see why these two scales would be different. I don't know if change is due to the new ggplot2 configuration or something in patchwork. Any chance the functionality could be returned?

Thanks so much!

library(patchwork)
library(ggplot2)
data("iris")

hist.group <- ggplot(iris, aes(Sepal.Length, fill = Species)) +
    geom_histogram()

density.group <- ggplot(iris, aes(Sepal.Length, fill = Species)) +
    geom_density(color = NA)

(hist.group | density.group) +
    plot_layout(guides = "collect") &
    theme(legend.position = "bottom")
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

Created on 2025-10-22 with reprex v2.1.1

Activity

  1. brueckmann commented on Nov 20, 2025

    @brueckmann

    May I add on, that on this page, https://patchwork.data-imaginist.com/reference/wrap_plots.html an example is mentioned ". If you modify default guide "position" with theme(legend.position=...) while also collecting guides you must apply that change to the overall patchwork (see example)." but I cannot find this example under the examples in the link mentioned.

    (Updated for clarity.)

  2. z3tt commented on Mar 16, 2026

    @z3tt

    I run into similar troubles here as well for examples I am quite sure would have worked before.

    Want to note that it works if geoms are the same, though:

    library(patchwork)
    library(ggplot2)
    data("iris")
    
    ## works if geom is the same
    hist.group <- ggplot(iris, aes(Sepal.Length, fill = Species)) +
      geom_histogram()
    
    (hist.group | hist.group) +
      plot_layout(guides = "collect") &
      theme(legend.position = "bottom")
    #> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
    #> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

    hist.group <- ggplot(iris, aes(Sepal.Length, fill = Species)) +
      geom_histogram()
    
    bar.group <- ggplot(iris, aes(Species, fill = Species)) +
      geom_bar()
    
    (hist.group | bar.group) +
      plot_layout(guides = "collect") &
      theme(legend.position = "bottom")
    #> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

    scatter.a <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
      geom_point()
        
    scatter.b <- ggplot(iris, aes(Petal.Length, Petal.Width, color = Species)) +
      geom_point()
    
    (scatter.a | scatter.b) +
      plot_layout(guides = "collect") &
      theme(legend.position = "bottom")

    scatter.c <- ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Sepal.Length)) +
      geom_point()
    
    scatter.d <- ggplot(iris, aes(Petal.Length, Petal.Width, color = Sepal.Length)) +
      geom_point()
    
    (scatter.c | scatter.d) +
      plot_layout(guides = "collect") &
      theme(legend.position = "bottom")

    Created on 2026-03-16 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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