我希望ggplot2主题使用一组特定的颜色,但是看不到如何避免在主题之外使用单独的线条。
我有此数据:
library(ggplot2)
mycars <- mtcars
mycars$cyl <- as.factor(mycars$cyl)
这是我使用的虚拟主题:
mytheme <- theme(panel.grid.major = element_line(size = 2))
ggplot(mycars, aes(x = wt, y = mpg)) +
geom_point(aes(color = cyl)) +
mytheme
我希望点颜色默认为我的自定义调色板:
mycolors <- c("deeppink", "chartreuse", "midnightblue")
我可以以某种方式将其添加到我的ggplot2主题中,以便在结尾时不再不断重复此额外的代码行:
ggplot(mycars, aes(x = wt, y = mpg)) +
geom_point(aes(color = cyl)) +
mytheme +
scale_color_manual(values = mycolors)
我试过了:
mytheme2 <- mytheme + scale_color_manual(values = mycolors)
但是得到了:
错误:不知道如何将scale_color_manual(values = mycolors)添加到主题对象
嗨,您可以将您的自定义元素放在中list
:
# Data
library("ggplot2")
mycars <- mtcars
mycars$cyl <- as.factor(mycars$cyl)
# Custom theme
mytheme <- theme(panel.grid.major = element_line(size = 2))
mycolors <- c("deeppink", "chartreuse", "midnightblue")
# put the elements in a list
mytheme2 <- list(mytheme, scale_color_manual(values = mycolors))
# plot
ggplot(mycars, aes(x = wt, y = mpg)) +
geom_point(aes(color = cyl)) +
mytheme2
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句