使用功能在ggplot中手动设置颜色

时间:2019-04-16 09:06:08

标签: r ggplot2 reprex

我有一个主题函数,我想在函数中添加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)) 加载库中的消息。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

        if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
            yourImage.image = pickedImage
        }else{
            print("Something went wrong!!")
        }
    }

reprex package(v0.2.0)于2019-04-16创建。

1 个答案:

答案 0 :(得分: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))