本文介绍了模拟 ggplot2 默认调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以使用什么函数来模拟 ggplot2 的默认调色板以获得所需的颜色数.例如,输入 3 将生成具有以下颜色的 HEX 颜色字符向量:
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 colors with these colors:
推荐答案
它只是围绕色轮等距的色调,从 15 开始:
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]
}
例如:
n = 4
cols = gg_color_hue(n)
dev.new(width = 4, height = 4)
plot(1:n, pch = 16, cex = 2, col = cols)
这篇关于模拟 ggplot2 默认调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!