Description
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.