I want my ggplot2 theme to use a specific set of colors, but don't see how to avoid a separate line outside of the theme.
I have this data:
library(ggplot2)
mycars <- mtcars
mycars$cyl <- as.factor(mycars$cyl)
And here's a dummy theme I plot with:
mytheme <- theme(panel.grid.major = element_line(size = 2))
ggplot(mycars, aes(x = wt, y = mpg)) +
geom_point(aes(color = cyl)) +
mytheme
I want the point colors to default to my custom palette:
mycolors <- c("deeppink", "chartreuse", "midnightblue")
Can I somehow add that to my ggplot2 theme so that I don't constantly repeat this extra line of code at the end:
ggplot(mycars, aes(x = wt, y = mpg)) +
geom_point(aes(color = cyl)) +
mytheme +
scale_color_manual(values = mycolors)
I tried:
mytheme2 <- mytheme + scale_color_manual(values = mycolors)
But got:
Error: Don't know how to add scale_color_manual(values = mycolors) to
a theme object
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…