Skip to content

Different behavior for collected legends when on right vs. on bottom if some graphs have position = "none" #423

Open
@shirewoman2

Description

@shirewoman2

I have a set of graphs where some legends need to include fill and other legends need to include color and shape. I want only the graphs with color and shape to show up in the shared legend.

Example graphs:
P1 <- ggplot(starwars, aes(x = height, fill = species, group = species)) + geom_density() + theme(legend.position = "none")

P2 <- ggplot(starwars, aes(x = height, y = mass, color = species, group = species)) + geom_point() + theme(legend.position = "right")

When I collect the legends and leave the legend position as the default, which is to put the legend on the right, this works and the shared legend only includes the color and shape.

P1 + P2 + plot_layout(guides = "collect")

However, when I collect the legends and put the legend on the bottom, it collects the legends for graphs where I had set legend.position to "none" as well.

P1 + P2 + plot_layout(guides = "collect") & theme(legend.position = "bottom")

Please let me know if I have missed something and am not specifying the legend position correctly.

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