$ b $ p
$ b $ p> mytheme2< - mytheme + scale_color_manual(values = mycolors)
但是得到:
$ b
解决方案
你好你可以把你的自定义元素放在列表中$ c $
#Data
library(ggplot2)
mycars< - mtcars
mycars $ cyl< - as.factor(mycars $ cyl)
#自定义主题
mytheme< - theme(panel.grid.major = element_line(大小= 2))
mycolors< - c(deeppink,chartreuse,midnightblue)
#将元素放入列表
mytheme2< - list(mytheme, scale_color_manual(values = mycolors))
#plot
ggplot(mycars,aes(x = wt,y = mpg))+
geom_point(aes(color = cyl)) +
mytheme2
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:
解决方案
Hi you can put your custom element in a 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
这篇关于将调色板与ggplot2主题相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!