Emulate ggplot2 default color palette

后端 未结 4 1466
猫巷女王i
猫巷女王i 2020-11-22 09:13

What function can I use to emulate ggplot2\'s default color palette for a desired number of colors. For example, an input of 3 would produce a character vector of HEX color

4条回答
  •  隐瞒了意图╮
    2020-11-22 09:37

    It is just equally spaced hues around the color wheel, starting from 15:

    gg_color_hue <- function(n) {
      hues = seq(15, 375, length = n + 1)
      hcl(h = hues, l = 65, c = 100)[1:n]
    }
    

    For example:

    n = 4
    cols = gg_color_hue(n)
    
    dev.new(width = 4, height = 4)
    plot(1:n, pch = 16, cex = 2, col = cols)
    

    enter image description here

提交回复
热议问题