Learn R Programming

ggguides (version 1.1.4)

legend_split: Force Legends to Stay Separate

Description

Force specified legends to remain separate by assigning different order values, preventing automatic merging.

Usage

legend_split(...)

Value

A guides specification that can be added to a plot.

Arguments

...

Aesthetic names (unquoted) to keep separate. E.g., colour, fill.

Details

By default, ggplot2 merges legends that have matching titles and labels. This function assigns different order values to each legend, which prevents automatic merging.

See Also

legend_merge, legend_order_guides

Examples

Run this code
library(ggplot2)

# Plot where colour and fill would normally merge
p <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl), fill = factor(cyl))) +
  geom_point(shape = 21, size = 3, stroke = 1.5) +
  labs(color = "Cylinders", fill = "Cylinders")

# Force separate legends
p + legend_split(colour, fill)

Run the code above in your browser using DataLab