How to create a number of the most distinctive colors in R?

I draw a categorical dataset and want to use distinctive colors to represent different categories. Given the number n , how can I get n number of distinctive MOST colors in R? Thank.

+118
colors r palette colorbrewer
Mar 07 '13 at 21:59
source share
8 answers

I joined all the quality palettes from the RColorBrewer package. Quality palettes should provide the X most distinctive colors each. Of course, mixing them together also brings similar colors into one palette, but this is 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)) 

color_Brewer_qual_60

Another solution: take all the R colors from the graphics devices and a sample from them. I removed the shades of gray as they are too similar. It gives 433 colors

 color = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(), invert = T)] 

set of 20 colors

 pie(rep(1,n), col=sample(color, n)) 

with 200 colors n = 200 :

 pie(rep(1,n), col=sample(color, n)) 

set of 200 flowers

+92
Oct. 15 '15 at 9:31
source share

Here are a few options:

  • Take 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) 
  • Take a look at the colorBrewer package (and website ). If you need diverging colors, select diverging on the site. For example,

     library(colorBrewer) brewer.pal(7, "BrBG") 
  • I want the hue website to produce many nice palettes. Again, just select the desired palette. For example, you can get rgb colors from a site and create 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))) 
+60
Mar 07 '13 at 22:06
source share

You can also try randomcoloR package :

 library(randomcoloR) n <- 20 palette <- distinctColorPalette(n) 

You can see that when rendering in a pie chart, a set of very different colors is selected, as suggested in other answers here:

 pie(rep(1, n), col=palette) 

enter image description here

Displayed in a pie chart with 50 colors:

 n <- 50 palette <- distinctColorPalette(n) pie(rep(1, n), col=palette) 

enter image description here

+27
Feb 20 '16 at 21:32
source share

Not an answer to an OP question, but it is worth mentioning that there is a viridis package that has good color palettes for serial data. They are equally perceived, safe for the blind and easy to print.

To get the palette, just install the package and use the viridis_pal() function. There are four options "A", "B", "C" and "D" to choose from

 install.packages("viridis") library(viridis) viridis_pal(option = "D")(n) # n = number of colors seeked 

enter image description here

enter image description here

enter image description here

There is also a great report explaining the complexity of good color maps on YouTube:

Best default color map for Matplotlib | SciPy 2015 | Nathaniel Smith and Stephen van der Walt

+15
Apr 16 '17 at
source share

You can use colorRampPalette from the base or RColorBrewer package:

Using colorRampPalette you can specify colors as follows:

 colorRampPalette(c("red", "green"))(5) # [1] "#FF0000" "#BF3F00" "#7F7F00" "#3FBF00" "#00FF00" 

You can also provide hexadecimal codes:

 colorRampPalette(c("#3794bf", "#FFFFFF", "#df8640"))(5) # [1] "#3794BF" "#9BC9DF" "#FFFFFF" "#EFC29F" "#DF8640" # Note that the mid color is the mid value... 

With RColorBrewer you can use colors from pre-existing palettes:

 require(RColorBrewer) brewer.pal(9, "Set1") # [1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00" "#FFFF33" "#A65628" "#F781BF" # [9] "#999999" 

Take a look at the RColorBrewer package for other palettes available. Hope this helps.

+13
Mar 07 '13 at 22:05
source share

I would recommend using an external source for large color palettes.

http://tools.medialab.sciences-po.fr/iwanthue/

has a service for compiling any palette size in accordance with 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 a common problem from the point of view of graphic designers and gives many examples of palettes used.

To contain a palette of RGB values, you just need to copy the values ​​into a vector, such as:

 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") 
+10
Nov 02 '16 at 16:06
source share

I found a site offering a list of 20 distinctive colors: 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 try!

+3
Nov 26 '18 at 2:15
source share

You can create a color set as follows:

 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 varied as possible. For these similar colors, they form a gradient so you can easily distinguish between them.

0
Jul 03 '19 at 16:08
source share



All Articles