Reputation: 93
I'm looking to remove or suppress levels of a ggplot2 legend without having to modify the data itself. Is this possible?
Here is some sample data:
library(ggplot2)
ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = factor(cyl))) +
scale_colour_manual(values = c("#85D4E3", "#CEAB07", "#29211F")) +
theme_bw()
To try and remove select levels, such as "8", from the legend I've tried things like the code modification below. This simply changes "8" to NA, and does not remove that level of the legend.
ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = factor(cyl))) +
scale_colour_manual(values = c("#85D4E3", "#CEAB07", "#29211F"), labels = c("4", "6", NULL)) +
theme_bw()
Upvotes: 1
Views: 138
Reputation: 903
You were close, instead of changing labels change the breaks
ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) +
geom_point(aes(color = factor(cyl))) +
scale_colour_manual(values = c("#85D4E3", "#CEAB07", "#29211F"), breaks = c("4", "6")) +
theme_bw()
Upvotes: 2