我创建了自己的主题,现在我也想标准化使用的颜色集。我尝试使用此提要中Viktor的答案中提供的列表解决方案来做到这一点:
Associate a color palette with ggplot2 theme
df <- mtcars
uwvPalet <- c("#0078D2","#003282","#C4D600")
theme_uwv <- function(base_size = 22, base_family = "Verdana"){theme_hc(base_size = base_size, base_family = base_family)%+replace%theme(plot.title = element_text(color = rgb(0, 120, 210)), complete = TRUE)}
theme_uwv2 <- list(theme_uwv, scale_color_manual(values = uwvPalet))
ggplot(df, aes(fill = cyl, x = am, y = mpg)) + geom_bar(position = "dodge", stat="identity") + theme_uwv2()
不幸的是,我得到了错误:
Error in theme_uwv2() : could not find function "theme_uwv2"
有人知道我该如何解决吗?
答案 0 :(得分:1)
以下内容对我有用。 theme_uwv2
需要将从theme_uwv()
返回的值作为列表元素,而不是函数本身。另外,您正在绘制一个fill
是主要颜色变量的图,因此出于演示目的,我已将scale_color_manual()
替换为scale_fill_manual()
。
library(ggplot2)
library(ggthemes)
df <- mtcars
uwvPalet <- c("#0078D2","#003282","#C4D600")
theme_uwv <- function(base_size = 22, base_family = "Verdana"){
theme_hc(base_size = base_size, base_family = base_family) %+replace%
theme(plot.title = element_text(color = rgb(0, 120, 210, maxColorValue = 255)),
complete = TRUE)}
theme_uwv2 <- list(theme_uwv(), scale_fill_manual(values = uwvPalet))
ggplot(df, aes(fill = as.factor(cyl), x = am, y = mpg)) +
geom_col(position = "dodge") +
ggtitle("test") +
theme_uwv2