创建自己的颜色方案,颜色选择

时间:2020-01-17 14:29:03

标签: r ggplot2

我想为ggplot2(考虑公司色彩)创建自己的默认配色方案/模板,如果用户加载程序包,该配色/模板将自动使用。

如果使用所有颜色,则系统正常工作。但是,如果仅使用颜色的子集,则默认情况下,ggplot2从头开始选择颜色。有没有办法通过其他功能选择颜色? (例如,从颜色列表中选择两种颜色时,请选择第一种和最后一种。对于三种颜色,请选择第一种,中间和最后一种,等等)

这应该自动完成,这样用户就不必自己指定颜色。

举例说明我到目前为止所做的事情:

library(ggplot2)
library(dplyr)

# in .onAttach as this is used within a package
my_colors <- c("red", "orange", "yellow", "green", "blue", "violet")

assign("scale_color_discrete", function(..., values = my_colors) scale_color_manual(..., values = as.character(values)), globalenv())
assign("scale_colour_discrete", function(..., values = my_colors) scale_colour_manual(..., values = as.character(values)), globalenv())
assign("scale_fill_discrete", function(..., values = my_colors) scale_fill_manual(..., values = as.character(values)), globalenv())

df <- mtcars %>% 
  mutate(carb = as.factor(carb))


df %>% 
  ggplot(aes(x = wt, y = mpg, color = carb)) + 
  geom_point()

好身材:全部使用了6种颜色(〜使用了我所有颜色的全部光谱)。

df %>% 
  filter(carb %in% c(2, 3, 4)) %>% 
  ggplot(aes(x = wt, y = mpg, color = carb)) +
  geom_point()

数字错误:使用前三种颜色。这里的目标是通过scale_color_manual()函数选择颜色1、3(或4),6,以“选择”整个光谱。

reprex package(v0.3.0)于2020-01-17创建

有什么想法如何选择正确的颜色?

1 个答案:

答案 0 :(得分:3)

我通过查看RColorBrewerggthemes的源代码中学到了这一点。我建议也去那里。您需要做的是使用discrete_scale。您还需要创建一个给定长度n的函数,该函数返回所需的颜色。

## Libraries
library(ggplot2)
library(dplyr)

# Your custom vector of colors
my_colors <- c("red", "orange", "yellow", "green", "blue", "violet")

# Function that returns a vector of n colors from your palette
# I just made this up... you can do what you want
custom_palette <- function(n) {

  pal_index <- seq_along(my_colors)

  if (n == 1) {
    pal_index = c(3)

  } else if (n == 2) {
    pal_index = c(1, 6)

  } else if (n == 3) {
    pal_index = c(1, 3, 6)

  } else if (n == 4) {
    pal_index = c(1, 3, 4, 6)

  } else if (n == 5) {
    pal_index = c(1, 2, 3, 4, 6)

  }

  my_colors[pal_index]

}

# Custom palette mapper
custom_pal <- function(type = "discrete", palette = custom_palette, direction = 1) {

  if (type == "discrete") {
    function(n) {
      pal <- palette(n)

      if (direction == -1) {
        pal <- rev(pal)
      }

      pal
    }
  } else if (type == "continuous") {

    pal <- palette()

    if (direction == -1) {
      pal <- rev(pal)
    }

    scales::gradient_n_pal(pal)
  }
}

# Function to implement using discrete_scale
# You need to do something similar for continuous
scale_color_custom <- function(..., direction = 1) {

  discrete_scale(aesthetics = "color", "my_colors", custom_pal("discrete", custom_palette, direction), ...)

}

# Assign as you did
# You do basically the same thing for "fill", but with the aesthetic changed in the function above
assign("scale_color_discrete", scale_color_custom, globalenv())
assign("scale_colour_discrete", scale_color_custom, globalenv())

df <- mtcars %>% 
  mutate(carb = as.factor(carb))

df %>% 
  filter(carb %in% c(2, 3, 4)) %>% 
  ggplot(aes(x = wt, y = mpg, color = carb)) +
  geom_point()

ggplot2 output

相关问题