转载自:https://www.jianshu.com/p/9cf0e5f52a87
这个需求真的太常见了!注意问题强调的几个关键词:一是快速,二是大量,三是差异明显。在生成大量元素比较图时要明显区分不同样本,比如宏基因组中的物种分析:
方法一:自定义
自定义颜色:优点是选择差异明显的颜色,缺点是费时费力,不知选多少种,眼睛都要挑花。
R的颜色板很多网站都可以查,随意搜一个贴上:https://www.sojson.com/rgb.html
R中颜色有600多种,可用自带函数colors()
查看。我们可以从中挑选。
仅通过名字我们可能不能直观感受到所选择的颜色,scales
包中有一个show_col
函数能以方格的形式展示颜色。
cb_palette <- c("#ed1299", "#09f9f5", "#246b93", "#cc8e12", "#d561dd", "#c93f00", "#ddd53e",
"#4aef7b", "#e86502", "#9ed84e", "#39ba30", "#6ad157", "#8249aa","#99db27",
"#e07233", "#ff523f","#ce2523", "#f7aa5d", "#cebb10", "#03827f", "#931635",
"#373bbf", "#a1ce4c", "#ef3bb6", "#d66551","#1a918f", "#ff66fc", "#2927c4",
"#7149af" ,"#57e559" ,"#8e3af4" ,"#f9a270" ,"#22547f", "#db5e92","#edd05e",
"#6f25e8", "#0dbc21", "#280f7a", "#6373ed", "#5b910f" ,"#7b34c1" ,"#0cf29a",
"#d80fc1","#dd27ce", "#07a301", "#167275", "#391c82", "#2baeb5","#925bea",
"#63ff4f")
方法二:RColorBrewer包
利用RColorBrewer包中的面板。
library(RColorBrewer)
display.brewer.all()
查看颜色面板有:
从中选择颜色区分差异大的面板,也是需要自己挑选,而且数目相对较少:
brewer.pal(9, "Set1") #只有9个
c(brewer.pal(9, "Set1") ,brewer.pal(9, "Set3") ) #也可结合,但颜色区分不大,数目也还是少
colorRampPalette(c("red", "green"))(5)
rainbow(60) #彩虹色很容易生成,但数目一多很难区分,因为是渐变的。
可以结合这些面板,稍微处理下筛选:
library(RColorBrewer)
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
#处理后有73种差异还比较明显的颜色,基本够用
col_vector = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
#看下中间60种颜色的效果
pie(rep(1,n), col=sample(col_vector, 60))
方法二得到的图:
方法三:randomcoloR
综合来说,这种方法是最合适的吧,也最省代码。但颜色太多的话,必定是有很多近似的。而且这种方法不能重复得到结果,因为是随机生成的嘛,即使设置种子也不行。
library(randomcoloR)
palette <- randomColor(count = 60) #随机生成60种颜色,其实里面有重复的
palette <- distinctColorPalette(60) #差异明显的60种
这个问题貌似很难完美解决,毕竟主要的颜色也就那么几种。以下是第三种方法得到的图:
最后提供几个颜色组合,从多到少:
library(RColorBrewer)
#433种
color433 = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)]
pie(rep(1,50), col=sample(color433, 50, replace = F))
#74种
qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
color74 = unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals)))
pie(rep(1,60), col=sample(color74, 60, replace = F))
#37种
color37 = c("#466791","#60bf37","#953ada","#4fbe6c","#ce49d3","#a7b43d","#5a51dc","#d49f36","#552095","#507f2d","#db37aa","#84b67c","#a06fda","#df462a","#5b83db","#c76c2d","#4f49a3","#82702d","#dd6bbb","#334c22","#d83979","#55baad","#dc4555","#62aad3","#8c3025","#417d61","#862977","#bba672","#403367","#da8a6d","#a79cd4","#71482c","#c689d0","#6b2940","#d593a7","#895c8b","#bd5975")
pie(rep(1,37), col=sample(color37, 37))
#20种
color20<-c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080', '#ffffff', '#000000')
pie(rep(1,20), col=sample(color20, 20))
自定义面板
matlab包的jet.colors(n)
生成从深蓝色到青色到黄色到深红色的n种颜色的序列。它类似于Python的matplotlib或MATLAB中的默认配色方案。 常作为渐变面板使用。
library(matlab)
mypal = jet.colors(1000)
pie(rep(1,1000), col=mypal)
Ref: https://stackoverflow.com/questions/15282580/how-to-generate-a-number-of-most-distinctive-colors-in-r
https://stackoverflow.com/questions/28109647/r-corrplot-colorlegend-change-range