Here's the function:
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
# Provides contrasting colors. Light.levels is the number of lightness | |
# levels to use before restarting. | |
category.colors <- function(n, contrast=90, light.levels=3) { | |
shades <- c() | |
color.levels = 16 | |
if(n > light.levels * color.levels) { | |
#recycle function | |
return(c(category.colors(light.levels * color.levels), | |
category.colors(n - light.levels * color.levels))) | |
} | |
hues <- function(n) { | |
sapply(0:(n-1), | |
function(x) | |
{90 *(x %% 4) + 22.5 * floor(x / 4)}) | |
} | |
while(n > color.levels) { | |
#recylce with lightness | |
shades <- c(shades, | |
hcl(h=hues(n), | |
l=(100 - (80 / light.levels) * | |
floor(n / color.levels)), | |
c=contrast )) | |
n <- n - color.levels | |
} | |
if(n > 0) { | |
shades <- c(shades, | |
hcl(h=hues(n), | |
c=contrast, l=70 )) | |
} | |
return(shades) | |
} |
Here's a comparison of the function (top) and the built-in rainbow function (bottom) which serves the same purpose.
Thank you so much for sharing!!!! I was looking for some palette which I could use with categories in a line plot! This palette is beautiful and useful. Thank you!
ReplyDelete