Skip to content

Shared legend feature stopped workin with ggplot2 v3.5.0 #202

@ycl6

Description

@ycl6

A pull request and commit made to ggplot2 last Dec tidyverse/ggplot2#5488 means from v3.5.0 onwards cowplot's shared legends feature will not work as before.

Below is the reproducible demo.

The plot_component_names() that looks for the "guide-box" pattern now returns multiple matches.
Because guide-box-right is the first match returned by plot_component_names(), therefore only the shared right-sided legend works.

library(ggplot2)
library(cowplot)

set.seed(1123)
dsamp = diamonds[sample(nrow(diamonds), 1000), ]

p1 = ggplot(dsamp, aes(carat, price, color = clarity)) +
    geom_point() + theme(legend.position="none")
p2 = ggplot(dsamp, aes(carat, depth, color = clarity)) +
    geom_point() + theme(legend.position="none")

prow = plot_grid(p1, p2, align = 'vh', nrow = 1)

legend1 = get_legend(
  p1 + theme(legend.position = "right")
)
#> Warning in get_plot_component(plot, "guide-box"): Multiple components found;
#> returning the first one. To return all, use `return_all = TRUE`.

plot_grid(prow, legend1, nrow = 1, rel_widths = c(7, 1))

legend2 = get_legend(
    p1 + guides(color = guide_legend(nrow = 1)) + 
            theme(legend.position = "bottom")
)
#> Warning in get_plot_component(plot, "guide-box"): Multiple components found;
#> returning the first one. To return all, use `return_all = TRUE`.

# Bottom legend not showing up
plot_grid(prow, legend2, ncol = 1, rel_heights = c(7, 1))

# get_legend, multiple 'guide-box' matches
# position = "right"
plot1 = as_gtable(p1 + theme(legend.position = "right"))
grob_names1 = plot_component_names(plot1)
grob_names1
#>  [1] "background"       "spacer"           "axis-l"           "spacer"          
#>  [5] "axis-t"           "panel"            "axis-b"           "spacer"          
#>  [9] "axis-r"           "spacer"           "xlab-t"           "xlab-b"          
#> [13] "ylab-l"           "ylab-r"           "guide-box-right"  "guide-box-left"  
#> [17] "guide-box-bottom" "guide-box-top"    "guide-box-inside" "subtitle"        
#> [21] "title"            "caption"
which(grepl("guide-box", grob_names1))
#> [1] 15 16 17 18 19

# position = "bottom"
plot2 = as_gtable(p1 + theme(legend.position = "bottom"))
grob_names2 = plot_component_names(plot2)
grob_names2
#>  [1] "background"       "spacer"           "axis-l"           "spacer"          
#>  [5] "axis-t"           "panel"            "axis-b"           "spacer"          
#>  [9] "axis-r"           "spacer"           "xlab-t"           "xlab-b"          
#> [13] "ylab-l"           "ylab-r"           "guide-box-right"  "guide-box-left"  
#> [17] "guide-box-bottom" "guide-box-top"    "guide-box-inside" "subtitle"        
#> [21] "title"            "caption"
which(grepl("guide-box", grob_names2))
#> [1] 15 16 17 18 19

Created on 2024-03-05 with reprex v2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions