Skip to content

An R package for generating attractive and distinctive colors.

Notifications You must be signed in to change notification settings

ronammar/randomcoloR

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CRAN_Status_Badge CRAN_Download_Badge

randomcoloR

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.

library(igraph)
library(randomcoloR)

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

We can specify a particular hue, such as red.

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

We can also get random colors with specific luminosity.

plot(erdos.renyi.game(k, 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.

library(dplyr)
library(ggplot2)
library(maps)

states_map <- map_data("state")

ggplot(states_map, aes(x=long, y=lat, group=group, fill=region)) +
  geom_polygon(color="black") +
  guides(fill=FALSE)

Which states are green?

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

ggplot(states_map, aes(x=long, y=lat, group=group, fill=region)) +
  geom_polygon(color="black") +
  scale_fill_manual(values=distinctColorPalette(length(unique(states_map$region)))) +
  guides(fill=FALSE)

Now, which states are green?

When using ggplot2, we can specify a custom color palette.

ggplot(mtcars, aes(x=disp, y=mpg, col=as.factor(gear))) +
  geom_point(size=5) +
  scale_colour_manual(values=randomColor(length(unique(mtcars$gear)), luminosity="light")) +
  theme_bw()

You can use the default color space.

set.seed(8675309)

scales::show_col(distinctColorPalette(12), labels=FALSE)

Or an alternate color space.

scales::show_col(distinctColorPalette(12, altCol=TRUE), labels=FALSE)

And you can preprocess the color space with t-SNE for improved color separation in some circumstances

scales::show_col(distinctColorPalette(12, altCol=TRUE, runTsne=TRUE), labels=FALSE)

Installation from Github

To install this package from Github via the R console, type:

devtools::install_git("https://github.com/ronammar/randomcoloR")

It's also on CRAN:

install.packages("randomcoloR")

About

An R package for generating attractive and distinctive colors.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published