Given following ggplot diagram,
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(colour = factor(cyl)))
If I want to change the labels of the legends, I can do the following:
p + scale_color_manual(labels = c("X", "Y", "Z"), values = 1:3)
But what should I do if I want to change the labels only and leave the colors unchanged? I've tried using the following formula:
p + scale_color_manual(labels = c("X", "Y", "Z"))
And it gives an error, saying: Error in f(...) : argument "values" is missing, with no default
.
Of course, I can achieve this by setting the factor cyl
:
mtcars$cyl = as.factor(mtcars$cyl, labels = c("X", "Y", "Z"))
ggplot(mtcars, aes(mpg, wt)) + geom_point(aes(colour = factor(cyl)))
But, I wondering whether it is possible to achieve this in scale_color_manual
or in some other ways.
How about scale_color_discrete
p + scale_color_discrete(labels=letters[1:3])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With