问题
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.
回答1:
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))
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))
with 200 colors n = 200
:
pie(rep(1,n), col=sample(color, n))
回答2:
Here are a few options:
Have a look at the
palette
function:palette(rainbow(6)) # six color rainbow (palette(gray(seq(0,.9,len = 25)))) #grey scale
And the
colorRampPalette
function:##Move from blue to red in four colours colorRampPalette(c("blue", "red"))( 4)
Look at the
colorBrewer
package (and website). If you want diverging colours, then select diverging on the site. For example,library(colorBrewer) brewer.pal(7, "BrBG")
The I want hue web site gives lots of nice palettes. Again, just select the palette that you need. For example, you can get the rgb colours from the site and make your own palette:
palette(c(rgb(170,93,152, maxColorValue=255), rgb(103,143,57, maxColorValue=255), rgb(196,95,46, maxColorValue=255), rgb(79,134,165, maxColorValue=255), rgb(205,71,103, maxColorValue=255), rgb(203,77,202, maxColorValue=255), rgb(115,113,206, maxColorValue=255)))
回答3:
You can also try the randomcoloR package:
library(randomcoloR)
n <- 20
palette <- distinctColorPalette(n)
You can see that a set of highly distinct colors are chosen when visualizing in a pie chart (as suggested by other answers here):
pie(rep(1, n), col=palette)
Shown in a pie chart with 50 colors:
n <- 50
palette <- distinctColorPalette(n)
pie(rep(1, n), col=palette)
回答4:
Not an answer to OP's question but it's worth mentioning that there is the viridis package which has good color palettes for sequential data. They are perceptually uniform, colorblind safe and printer-friendly.
To get the palette, simply install the package and use the function viridis_pal()
. There are four options "A", "B", "C" and "D" to choose
install.packages("viridis")
library(viridis)
viridis_pal(option = "D")(n) # n = number of colors seeked

There is also an excellent talk explaining the complexity of good colormaps on YouTube:
A Better Default Colormap for Matplotlib | SciPy 2015 | Nathaniel Smith and Stéfan van der Walt
回答5:
You can use colorRampPalette
from base or RColorBrewer
package:
With colorRampPalette
, you can specify colours as follows:
colorRampPalette(c("red", "green"))(5)
# [1] "#FF0000" "#BF3F00" "#7F7F00" "#3FBF00" "#00FF00"
You can alternatively provide hex codes as well:
colorRampPalette(c("#3794bf", "#FFFFFF", "#df8640"))(5)
# [1] "#3794BF" "#9BC9DF" "#FFFFFF" "#EFC29F" "#DF8640"
# Note that the mid color is the mid value...
With RColorBrewer
you could use colors from pre-existing palettes:
require(RColorBrewer)
brewer.pal(9, "Set1")
# [1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00" "#FFFF33" "#A65628" "#F781BF"
# [9] "#999999"
Look at RColorBrewer
package for other available palettes. Hope this helps.
回答6:
I would recomend to use an external source for large color palettes.
http://tools.medialab.sciences-po.fr/iwanthue/
has a service to compose any size of palette according to various parameters and
https://graphicdesign.stackexchange.com/questions/3682/where-can-i-find-a-large-palette-set-of-contrasting-colors-for-coloring-many-d/3815
discusses the generic problem from a graphics designers perspective and gives lots of examples of usable palettes.
To comprise a palette from RGB values you just have to copy the values in a vector as in e.g.:
colors37 = 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")
回答7:
I found a website offering a list of 20 distinctive colours: https://sashat.me/2017/01/11/list-of-20-simple-distinct-colors/
col_vector<-c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080', '#ffffff', '#000000')
You can have a try!
回答8:
You can generate a set of colors like this:
myCol = c("pink1", "violet", "mediumpurple1", "slateblue1", "purple", "purple3",
"turquoise2", "skyblue", "steelblue", "blue2", "navyblue",
"orange", "tomato", "coral2", "palevioletred", "violetred", "red2",
"springgreen2", "yellowgreen", "palegreen4",
"wheat2", "tan", "tan2", "tan3", "brown",
"grey70", "grey50", "grey30")
These colors are as distinct as possible. For those similar colors, they form a gradient so that you can easily tell the differences between them.
来源:https://stackoverflow.com/questions/15282580/how-to-generate-a-number-of-most-distinctive-colors-in-r