Using Color Palettes in R

If you like me, are unable to decide whether blue or brown is a better color for graph- color palettes in R are a big help for aesthetically acceptable alternatives.

Using the same graphs, I choose the 5 main kinds of color palettes, using them is as easy as specifying the col= parameter in graphical display in Base Graphs. And I modified the n parameter for number of colors to be used- you can specify more or less depending how much you want the gradient or difference in colors to be.

> hist(VADeaths,col=heat.colors(7))

> hist(VADeaths,col=terrain.colors(7))


> hist(VADeaths,col=topo.colors(8))
 # I increased the colors to 8, since there are 8 bins to prevent repetion

> hist(VADeaths,col=cm.colors(8))
 
> hist(VADeaths,col=cm.colors(10))

# I increased the colors to 10, to showcase the difference in using less or more colors

finally I went to Pretty R at http://www.inside-r.org/pretty-r/tool and made my code more colorful-

I guess on my wishlist/dreamlist/amazon gift list is for GGplot 3/Deducer to have automated color palettes, or even the long promised coming in 2011 Revolution GUI (promised in 2010 roadmap) http://blog.revolutionanalytics.com/2010/05/revolutions-2010-roadmap.html

 http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/palettes.htmlFromR Documentation

Palettes {grDevices}

Color Palettes

Description

Create a vector of n contiguous colors.

Usage

rainbow(n, s = 1, v = 1, start = 0, end = max(1,n - 1)/n,
        gamma = 1, alpha = 1)
heat.colors(n, alpha = 1)
terrain.colors(n, alpha = 1)
topo.colors(n, alpha = 1)
cm.colors(n, alpha = 1)

Arguments

n the number of colors (≥ 1) to be in the palette.
s,v the ‘saturation’ and ‘value’ to be used to complete the HSV color descriptions.
start the (corrected) hue in [0,1] at which the rainbow begins.
end the (corrected) hue in [0,1] at which the rainbow ends.
gamma the gamma correction, see argument gamma in hsv. Deprecated.
alpha the alpha transparency, a number in [0,1], see argument alpha in hsv.

Details

Conceptually, all of these functions actually use (parts of) a line cut out of the 3-dimensional color space, parametrized by hsv(h,s,v), and hence, equispaced hues in RGB space tend to cluster at the red, green and blue primaries.

Some applications such as contouring require a palette of colors which do not wrap around to give a final color close to the starting one.

With rainbow, the parameters start and end can be used to specify particular subranges of hues. The following values can be used when generating such a subrange: red=0, yellow=1/6, green=2/6, cyan=3/6, blue=4/6and magenta=5/6.

Value

A character vector, cv, of color names. This can be used either to create a user–defined color palette for subsequent graphics by palette(cv), a col= specification in graphics functions or in par.

See Also

colorspalettehsvhclrgbgray and col2rgb for translating to RGB numbers.

Author: Ajay Ohri

http://about.me/ajayohri

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: