如何在 R 中生成一些最独特的颜色? [英] How to generate a number of most distinctive colors in R?
问题描述
我正在绘制一个分类数据集,并希望使用不同的颜色来表示不同的类别.给定一个数字 n
,如何在 R 中获得 n
个最独特的颜色?谢谢.
I am plotting a categorical dataset and want to use distinctive colors to represent different categories. Given a number n
, how can I get n
number of MOST distinctive colors in R? Thanks.
推荐答案
我加入了 RColorBrewer
包中的所有定性调色板.定性调色板应该提供 X 种最独特的颜色.当然,将它们混合到一个调色板中也会有相似的颜色,但这是我能得到的最好的结果(74 种颜色).
I joined all qualitative palettes from RColorBrewer
package. Qualitative palettes are supposed to provide X most distinctive colours each. Of course, mixing them joins into one palette also similar colours, but that's the best I can get (74 colors).
library(RColorBrewer)
n <- 60
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
pie(rep(1,n), col=sample(col_vector, n))
其他解决方案是:从图形设备中获取所有 R 颜色并从中取样.我删除了灰色阴影,因为它们太相似了.这给出了 433 种颜色
Other solution is: take all R colors from graphical devices and sample from them. I removed shades of grey as they are too similar. This gives 433 colors
color = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)]
pie(rep(1,n), col=sample(color, n))
有 200 种颜色 n = 200
:
with 200 colors n = 200
:
pie(rep(1,n), col=sample(color, n))
这篇关于如何在 R 中生成一些最独特的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!