How to generate a number of most distinctive colors in R?

color-palette, colorbrewer, colors, palette, r

Solution

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

Problem

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.

Original source