R colorspace Package

Using the colorspace package in R

The open source software R provides a package called colorspace (missing reference) which uses Ihaka’s colorspace library. The package offers some presetted color palettes (rainbow_hcl, terrain_hcl, heat_hcl, …) to compare to the default RGB color palettes. Furthermore there is a graphical user interface (GUI) where you can design your own color palette. The function therefore is called choose_palette() (needs some dependent packages) and use them for your own work.

The first thing you need is an R installation on your computer. The installation packages for all available operating systems (Windows, OSX, Linux) can be found on the http://cran.r-project.org. The installation is no adventure and the R base version just needs a few MB of free space. For beginners the R-Studio GUI is recommended (or a similar R editor).

After R is successfully installed on your system you can install optional packages like the colorspace package. You can simply do that over your GUI interface or install them by using the following code line:

install.packages('colorspace')

R asks you for a mirror where to download the package and automatically installs other dependent packages. After this step you should allready be able to use the colorspace package. There is a minimal example below which produces a scatter plot to show how it basically works.

## Generating some synthetic data
x1 <- rnorm(20,mean=2); y1 <- rnorm(20,mean=1)
x2 <- rnorm(20,mean=3); y2 <- rnorm(20,mean=2)
x3 <- rnorm(20,mean=1); y3 <- rnorm(20,mean=3)
 
## Loading the library colorspace
library('colorspace')
 
## Call choose_palette. Note: interactive GUI opens
pal <- choose_palette()
 
## Picking 3 different colors out of your palette
colors_A <- pal(3)
## And picking 3 colors out of the HCL rainbow palette
colors_B <- rainbow_hcl(3)
 
## Create a plot containing 2 subplots
par(mfrow=c(1,2))
 
## Makes a scatterplot with your own choosen colors
plot(c(x1,x2,x3),c(y1,y2,y3),type='n',xlab='x values',
     ylab='y values',main='Example A: scatterplot')
 
points(x1,y1,col=colors_A[1])
points(x2,y2,col=colors_A[2])
points(x3,y3,col=colors_A[3])
 
## The same plot with the rainbow_hcl colors
plot(c(x1,x2,x3),c(y1,y2,y3),type='n',xlab='x values',
     ylab='y values',main='Example B: scatterplot')
 
points(x1,y1,col=colors_B[1])
points(x2,y2,col=colors_B[2])
points(x3,y3,col=colors_B[3])

R-Project: colorspace package

References