[R] how to quickly generate a number of distinct color difference?

The demand is really too common! Pay attention to issues highlighted a few key words: First, fast, and second, a large number of third is significantly different. When a large amount of elements to be clearly distinguished FIG comparison of different samples, such as species of metagenome analysis:
image.png

Method One: Customize

Custom colors: the advantage of selecting the color differences are significant, the disadvantage is time-consuming, I do not know how many election, the eyes have to cross-stitch.
R color plates can check many sites, free to search a paste: https://www.sojson.com/rgb.html

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")

Method two: RColorBrewer package

RColorBrewer panel using the package.

library(RColorBrewer)
display.brewer.all()

View Color panel:
image.png
choose from a large panel of color-coded differences, but also need to choose, and a relatively small number:

brewer.pal(9, "Set1") #只有9个
c(brewer.pal(9, "Set1") ,brewer.pal(9, "Set3") ) #也可结合,但颜色区分不大,数目也还是少
colorRampPalette(c("red", "green"))(5)

rainbow(60) #彩虹色很容易生成,但数目一多很难区分,因为是渐变的。

These panels can be incorporated, at a slightly screening process:

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))

image.png
FIG Method two to obtain:
image.png

Method three: randomcoloR

In summary, this method is most appropriate now, most provincial codes. But too many colors, then there must be a lot similar. This method can not be repeated and the results obtained, as is a randomly generated Well, even if the seed too.

library(randomcoloR)
palette <- randomColor(count = 60)  #随机生成60种颜色,其实里面有重复的
palette <- distinctColorPalette(60) #差异明显的60种

Seemingly difficult to solve this problem perfect, after all, the main color also so few. The following is a third method of FIG obtained:
image.png

Ref: https://stackoverflow.com/questions/15282580/how-to-generate-a-number-of-most-distinctive-colors-in-r

Guess you like

Origin www.cnblogs.com/jessepeng/p/11361228.html