Skip to content

guides = "collect" does not work when using different themes #466

Description

@z3tt

Hi Thomas,

I just noticed that applying two different themes breaks the guide merging logic.

library(ggplot2)
library(patchwork)

p1 <- ggplot(mpg, aes(hwy, displ, color = drv)) +
    geom_point()

p2 <- ggplot(mpg, aes(cty, displ, color = drv)) +
    geom_point() + 
    theme_minimal()

(p1 + p2) + plot_layout(guides = "collect") &
    theme(legend.position = "bottom")

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

Guess that's a decision (or maybe also technical difficulty?) as it'd need to decide which theme to apply to the legend.

Not sure it ever worked before but it would be kinda neat to have an option to combine differently themed plots. Do you think that's possible?

Activity

  1. teunbrand commented on Mar 16, 2026

    @teunbrand
    Collaborator

    Hi Cédric,

    Patchwork merges guides that are identical on the grid level. It doesn't know about the ggplot2-level properties of guides (for example it doesn't know if they share breaks or glyphs or anything of the sort).

    To merge legends affected by different themes, you need to force them to use the same theme (or otherwise ensure they're styled identically).

    In the example below, we're forcing the first plot's legend to adapt to the same theme as the second plot.

    library(ggplot2)
    library(patchwork)
    
    p1 <- ggplot(mpg, aes(hwy, displ, color = drv)) +
      geom_point() +
      guides(color = guide_legend(theme = theme_minimal()))
    
    p2 <- ggplot(mpg, aes(cty, displ, color = drv)) +
      geom_point() + 
      theme_minimal()
    
    (p1 + p2) + plot_layout(guides = "collect") &
      theme(legend.position = "bottom")

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

  2. z3tt commented on Mar 16, 2026

    @z3tt
    Author

    Nice example for the guide_legend() functionality! While preparing a "real-life" use case I also realized that one can use the same theme and then modify one plot as well.

    Closing this issue, but adding this example for the sake of completeness:

    library(ggplot2)
    library(patchwork)
    
    g1 <- ggplot(mpg, aes(hwy, displ, color = drv)) +
        geom_point() +
        theme_minimal()
    
    g2 <- ggplot(mpg, aes(drv, displ,  color = drv)) +
        geom_boxplot(color = "grey30") +
        geom_jitter(width = .1) + 
        theme_minimal()
    
    ## modify on the fly
    ((g1 + theme(panel.border = element_rect(color = "grey30", fill = NA))) + g2) + 
        plot_layout(guides = "collect") &
        theme(legend.position = "bottom")

    ## or change plot directly
    g1b <- ggplot(mpg, aes(hwy, displ, color = drv)) +
        geom_point() +
        theme_minimal() +
        theme(panel.border = element_rect(color = "grey30", fill = NA))
    
    (g1b + g2) + 
        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