使用函数在ggplot中手动设置颜色

3

我有一个主题函数,想在函数内部添加scale_fill_manual(values=x)并更新所有图表,但如果我在my_theme函数内添加此行,则无法正常工作。另外,我不知道如何使用reprex抑制加载库时的消息。

library(tidyverse, quietly = TRUE)

# Here I just create my HEX colors from RGB colors.  
x <-   tibble(r = c(187, 6, 226, 78, 221),
              g = c(180, 110, 223, 118, 128),
              b = c(135, 159, 204, 109, 71))


x <- modify(x, as.hexmode) %>%
  unite(r, g, b, col="hex", sep="") %>%
  map_df(~paste0("#", .x)) %>%
  pull()

# This is my theme function and I would like to add scale_fill_manual, 
# but then it does not work. I have tried different combinations.
my_theme <- function(){
    theme_minimal() +
    theme(title = element_text(color = "gray25"),
          plot.subtitle = element_text(size=12),
          plot.title = element_text(size=12),
          plot.caption = element_text(color= "gray30")) 

}

# But if I just add it in a separate line then it works. 
mpg %>% 
  ggplot() +
  geom_bar(aes(x = fct_infreq(class), fill=factor(cyl)), color="black", width = 0.5) +
  labs(title= "CK farver") +
  my_theme() +
  scale_fill_manual(values = x) +
  theme(axis.text.x = element_text(angle = -90, hjust = 0)) 

该示例是由 reprex包 (v0.2.0) 创建于2019年4月16日。

1个回答

3

除非你想将其他主题规范传递到my_theme中,否则我认为没有必要将其定义为函数。使用列表即可。

以下内容可以满足您的需求:

my_theme2 <- list(
  theme_minimal() +
    theme(title = element_text(color = "gray25"),
          plot.subtitle = element_text(size=12),
          plot.title = element_text(size=12),
          plot.caption = element_text(color= "gray30")) ,
  scale_fill_manual(values = x)
)

mpg %>% 
  ggplot() +
  geom_bar(aes(x = fct_infreq(class), fill=factor(cyl)), color="black", width = 0.5) +
  labs(title= "CK farver") +
  my_theme2 + 
  theme(axis.text.x = element_text(angle = -90, hjust = 0))

(输出图形未显示,因为它与问题中的图形相同)

使用以上函数版本进行编辑:

my_theme3 <- function(...){
  list(
    theme_minimal() +
      theme(title = element_text(color = "gray25"),
            plot.subtitle = element_text(size=12),
            plot.title = element_text(size=12),
            plot.caption = element_text(color= "gray30"),
            ...) ,
    scale_fill_manual(values = x)
  )
}

# same plot as before
mpg %>% 
  ggplot() +
  geom_bar(aes(x = fct_infreq(class), fill=factor(cyl)), color="black", width = 0.5) +
  labs(title= "CK farver") +
  my_theme3() +
  theme(axis.text.x = element_text(angle = -90, hjust = 0))

# if you want to add other tweaks to the theme, e.g. red labels, different legend position
mpg %>% 
  ggplot() +
  geom_bar(aes(x = fct_infreq(class), fill=factor(cyl)), color="black", width = 0.5) +
  labs(title= "CK farver") +
  my_theme3(axis.text = element_text(color = "red"),
            legend.position = "bottom") +
  theme(axis.text.x = element_text(angle = -90, hjust = 0))

谢谢,好的它有效了。但是为什么你把“scale_fill_manual”放在“theme”内而不是独立一行并用“+”分开呢? - xhr489
@David scale_* 是此处列表的一个单独组件。如果它们在列表中,可以一次性向 ggplot 对象添加多个组件。您可以在 ?%+% 中的最后一个示例中看到这一点。如果您想查看底层代码,则相关方法在 此处 中定义,即 ggplot2:::ggplot_add.list - Z.Lin
@ Z.Lin:非常感谢,但如果我想保留我的函数怎么办?您能否也提供函数形式的解决方案? - xhr489
@David 当然。已编辑完毕。 - Z.Lin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接