Togaware DATA MINING
Desktop Survival Guide
by Graham Williams
Google

Colour

R provides a collection of predefined colours and supports the generation of colours using numerous colour models. Colours are useful in plots to help distinguish different elements. The RColorBrewer provides a number of palettes, limited to 12 colours. It is generally thought difficult to distinguish more than about 12 colours.

The list of named colours is obtained from the colours function:



> head(colours())



[1] "white"         "aliceblue"     "antiquewhite" 
[4] "antiquewhite1" "antiquewhite2" "antiquewhite3"



> tail(colours())



[1] "yellow"      "yellow1"     "yellow2"     "yellow3"    
[5] "yellow4"     "yellowgreen"

There's 657 colours to choose from!

You can make a selection of colours using the colors.plot function from the epitools package. A plot is displayed with the range of named colours, and we can click on different colours with the left mouse button (we hear a beep for each click), then click the right mouse button when done. The list of chosen colours will be returned from the function.



> library(epitools)
> mycols <- as.character(colors.plot(locator = TRUE)$color.names)
> mycols



[1] "tomato3"         "wheat1"          "indianred2"     
[4] "khaki"           "darkolivegreen1" "cyan"           
[7] "lightpink"

Image dmsurvivor-graphics:colors_plot_fig

To explore automatic selection of colour ranges:



> library(colorRamps)
> blue2green2red(15)



 [1] "#0000CC" "#0000FF" "#0055FF" "#00AAFF" "#00FFFF"
 [6] "#2BFFD5" "#55FFAA" "#80FF80" "#AAFF55" "#D4FF2B"
[11] "#FFFF00" "#FFAA00" "#FF5500" "#FF0000" "#CC0000"



> image(matrix(1:150, 10), col = blue2green2red(15))

Image dmsurvivor-graphics:color_ramps

The Roption[]col option of a plot is used to change any default colours used by a plot. You can supply a list of integers which will index the output of a call to the palette function. The default palette is:



> palette()



[1] "black"   "red"     "green3"  "blue"    "cyan"   
[6] "magenta" "yellow"  "gray"



> image(matrix(1:80, 10), col = palette())

Image dmsurvivor-graphics:palette

You can generate a contiguous colour palette using cm.colors.



> cm.colors(10)



 [1] "#80FFFFFF" "#99FFFFFF" "#B3FFFFFF" "#CCFFFFFF"
 [5] "#E6FFFFFF" "#FFE6FFFF" "#FFCCFFFF" "#FFB3FFFF"
 [9] "#FF99FFFF" "#FF80FFFF"



> image(matrix(1:100, 10), col = cm.colors(10))

Image dmsurvivor-graphics:cm_colors

Similarly, to generate a sequence of colours from a rainbow:



> rainbow(10)



 [1] "#FF0000FF" "#FF9900FF" "#CCFF00FF" "#33FF00FF"
 [5] "#00FF66FF" "#00FFFFFF" "#0066FFFF" "#3300FFFF"
 [9] "#CC00FFFF" "#FF0099FF"



> image(matrix(1:100, 10), col = rainbow(10))

Image dmsurvivor-graphics:rainbow

To generate a sequence of six grays you can use the gray function:



> gray(seq(0.1, 0.9, len=6))



[1] "#1A1A1A" "#424242" "#6B6B6B" "#949494" "#BDBDBD"
[6] "#E6E6E6"



> image(matrix(1:60, 10), col = gray(seq(0.1, 0.9, len=6)))

Image dmsurvivor-graphics:gray

To generate a reasonable collection of 15 colours:



> library(RColorBrewer)
> mycolors <- brewer.pal(12,"Set3")
> mycolors <- c(mycolors, "blue", "green", "yellow")
> mycolors



 [1] "#8DD3C7" "#FFFFB3" "#BEBADA" "#FB8072" "#80B1D3"
 [6] "#FDB462" "#B3DE69" "#FCCDE5" "#D9D9D9" "#BC80BD"
[11] "#CCEBC5" "#FFED6F" "blue"    "green"   "yellow"



> image(matrix(1:150, 10), col = mycolors)

Image dmsurvivor-graphics:fifteen_colors

Copyright © Togaware Pty Ltd
Support further development through the purchase of the PDF version of the book.
The PDF version is a formatted comprehensive draft book (with over 800 pages).
Brought to you by Togaware. This page generated: Sunday, 22 August 2010