Learn R Programming

ggguides (version 1.1.4)

legend_merge: Force Legends to Merge

Description

Force specified legends to merge together by setting them to the same order. Legends will only merge if they have matching labels (same factor levels or break values).

Usage

legend_merge(...)

Value

A guides specification that can be added to a plot.

Arguments

...

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

Details

ggplot2 automatically merges legends when they have the same title and matching labels. This function ensures legends have the same order value (order = 0), which is a prerequisite for merging.

If legends still don't merge after using this function, ensure:

  • Both aesthetics map to the same variable

  • The legends have identical titles (use labs())

  • The breaks/labels are identical

See Also

legend_split, legend_order_guides

Examples

Run this code
library(ggplot2)

# Plot where colour and fill map to the same variable
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 merge (they should merge automatically if labels match)
p + legend_merge(colour, fill)

Run the code above in your browser using DataLab