Reputation: 1276
I would like to remove some legend entries in my ggplot
.
Previously, this was possible by defining the breaks
parameter in the relevant scale
, as explained here. However, that functionality seems to be broken with a recent update.
Example:
ggplot(mtcars) +
geom_col(aes(x = 1:nrow(mtcars), y = disp, fill = paste(cyl))) +
scale_fill_manual(values = c("grey50", "grey60", "grey70"),
breaks = c("4"))
While this code snippet only diplays one legend item, as intended, it does not allow to control the colours of the non-displayed elements.
With the breaks
attribute omitted:
Upvotes: 1
Views: 1127
Reputation: 84709
Use a named values
argument:
ggplot(mtcars) +
geom_col(aes(x = 1:nrow(mtcars), y = disp, fill = paste(cyl))) +
scale_fill_manual(values = c("4" = "red", "6" = "green", "8" = "blue"),
breaks = c("4"))
Upvotes: 4