Last active
August 1, 2020 04:53
-
-
Save darokun/80298bb93d01ae3f8f5c2e205ed5b4d1 to your computer and use it in GitHub Desktop.
Generating different and distinct colors in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(RColorBrewer) | |
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))) | |
#check | |
myunif <- runif(40, 1, 1) | |
barplot(myunif, col = col_vector) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some of the colors returned above (after removing exact duplicates) are very similar to each other. (e.g. "#E31A1C" and "#E41A1C", which I can't tell apart visually). Rather than rely on chance to avoid picking such pairs colors, I wrote the following little function that discards the color most similar to another until only the desired number of colors are left:
It computes distances between colors in RGB space. Using a perceptual space like Lab would be better.