Generate Attractive Random Colors

Simple methods to generate attractive random colors. The random colors are from a wrapper of 'randomColor.js' <>. In addition, it also generates optimally distinct colors based on k-means (inspired by 'IWantHue' <>).

An R package for generating attractive and distinctive colors.

The randomColor() function is ported from randomColor.js.

Let's quickly get some pretty random colors.

k <- 200
plot(, 0.1), vertex.label=NA,
     edge.lty="blank", vertex.color=randomColor(k))

We can specify a particular hue, such as red.

plot(, 0.1), vertex.label=NA,
     edge.lty="blank", vertex.color=randomColor(k, hue="red"))

We can also get random colors with specific luminosity.

plot(, 0.1), vertex.label=NA,
     edge.lty="blank", vertex.color=randomColor(k, luminosity="light"))

We can also ask for a set of optimally distinct colors so that colors in our plot are not too similar. If we use ggplot2 to select the color space for our states in the map below, we get many similar colors.

states_map <- map_data("state")
ggplot(states_map, aes(x=long, y=lat, group=group)) +
  geom_polygon(aes(fill=region), color="black") +

Which states are green?

Instead, let's find the most distinctive set of colors for all states.

s <- unique(states_map$region)
df <- data.frame(region=s, newColor=distinctColorPalette(length(s)),
states_map <- left_join(states_map, df, by="region")
ggplot(states_map, aes(x=long, y=lat, group=group)) +
  geom_polygon(fill=states_map$newColor, color="black")

Now, which states are green?

To install this package from the R console, type:



randomcoloR 1.1.0


  • Alternate color space for distinct colors

  • t-SNE preprocessing of color space for improved color separation


  • distinctColorPalette() no longer outputs a named vector, improving compatibility with ggplot2 (where color names are meaningful)

  • Shrinks original color space for improved performance (especially with t-SNE and k-medoids)

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


1.1.0 by Ron Ammar, a year ago

Report a bug at

Browse source code at

Authors: Ron Ammar

Documentation:   PDF Manual  

CC0 license

Imports colorspace, stringr, V8, stats, methods, scales, Rtsne, grDevices, cluster

Imported by BiBitR, CRPClustering, CSFA, RSDA, SanzCircos, fdq, lessR.

See at CRAN