Learn R Programming

ggguides (version 1.1.4)

legend_select: Keep Only Specific Legends

Description

Show only the specified legends and hide all others. This is the inverse of legend_hide.

Usage

legend_select(...)

Value

A guides specification that can be added to a plot, or NULL

if nothing needs to be hidden.

Arguments

...

Aesthetic names (unquoted) to keep. All other legends will be hidden. Common values: colour, fill, size, shape, linetype, alpha.

See Also

legend_hide, legend_none

Examples

Run this code
library(ggplot2)

# Plot with multiple legends
p <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl), size = hp,
                         shape = factor(am))) +
  geom_point()

# Keep only the colour legend
p + legend_select(colour)

# Keep colour and shape, hide size
p + legend_select(colour, shape)

Run the code above in your browser using DataLab