| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

RTip - Colours

Page history last edited by PBworks 17 years, 10 months ago

Colours

 

Adding colour to graphics is a well known trick to improve their impact and ease of use. R comes with a standard "palette", but there are several colour packages that are useful and you can also easily create your own.

 

This file contains some R code which illustrates some of the different packages that are avaialable. In some come cases we have written our own code; but mainly it is just taken from the "Examples" from the help page - it is useful to have the code in one place we hope.

 

Palettes

 

put in code to let text wrap around the picture;

 

A paragraph with an image. The align attribute of the image is set to "left". The image will float to the left of this text.

 

 

 

 

If you type the following code

plot(1:10, col=1:10, pch=19, cex=3)

you get the 10 dots of different colours; note the last two are repeated, they are the same as the first two. This is because the standard palette in R contains 8 colours: black, red, green3, blue, cyan, magenta, yellow, gray.

 

R allows you to change the palette. To do this you use the palette() function. See the [http://stat.ethz.ch/R-manual/R-patched/library/grDevices/html/palette.htmlhelp] for this for more details.

 

Suppose we want to set up a bespoke palette. For example your company may have one (Lloyds does). The colours below are specified by giving their RGB values, the syntax is #RRGGBB (see below for more), where RR is the amount of red ranging from 0 to 255 in hexadecimal (so 255=FF, 10=A etc), GG=green, BB=blue.

 

 

 

If you run this code you will see that the palette has changed to the new colours. This time there are only 5 so the colours are fully recyled twice.

 

newPalette<-c("#C58ADA","#E84310","#76B5EE","#62C35B","#E0D60C")

palette(newPalette)

plot(1:10, col=1:10, pch=19, cex=3)

The colours above were chosen by playing with the custom tab in excel (if you insert a rectangle and then format auto shape/ colour/More colours/custom - this allows you to move the cursor over colours and see the resulting rgb - it is a good way to get a feel for how rgb works (see below).

 

Custom functions

 

The following code shows how the function colorRampPalette will linearly interpolate in smooth fashion between a smaller palette (the same one we created above).

newPalette<-c("#C58ADA","#E84310","#76B5EE","#62C35B","#E0D60C")

newPalette<-colorRampPalette(newPalette, space="Lab")

palette(newPalette(20))

plot(1:20, col=1:20, pch=19, cex=3)

This is useful for contour maps, use this code:

filled.contour(volcano, color = colorRampPalette(newPalette, space = "Lab"), asp = 1)

to create this picture:

 

 

Another function which can interpolate between colours is "colorpanel" in the gplots package. But you can only interpolate between 3 colours here which doesnt seem as powerful as colorRampPalette. This is typical of R, there are often several ways to achieve the same result; it doesnt matter too much which you choose as long as you are happy with the results.

 

 

There are several functions/packages with a selection of palettes. For example brewer.pal in the RColorBrewer package and the functions: rainbow, rich.colours and heat.colours. See for example:

require(RColorBrewer)

palette(brewer.pal(9,"Greens"))

plot(1:10, col=1:10, pch=19, cex=3)

note, there are a maximum of 9 colours in "Greens" - so some recycling is forced on you here...

 

Note you dont need to set up a new palette; if you want to use the colours as a one off you can use them directly within a plot function, see the following code (note we already did that above too).

require(gplots)

plot(1:10, col=rich.colors(10), pch=19, cex=3)

note how rich.colors (and some others) allow you to specify the number of colours you want - it will then provide you a smoothing varying set.

 

 

rgb

 

The rgb syntax for specifying colours is fairly simple to understand.

The following code draws a cube in rgb space to see which colours are produced for various combinations:

 

 

require(

cubedraw <- function(res3d, min = 0, max = 255, cex = 2, text. = FALSE)

{

## Purpose: Draw nice cube with corners

cube01 <- rbind(c(0,0,1), 0, c(1,0,0), c(1,1,0), 1, c(0,1,1), # < 6 outer

c(1,0,1), c(0,1,0)) # <- "inner": fore- & back-ground

cub <- min + (max-min)* cube01

## visibile corners + lines:

res3d$points3d(cubc(1:6,1,7,3,7,5) ,, cex = cex, type = 'b', lty = 1,col="darkgrey")

## hidden corner + lines

res3d$points3d(cubc(2,8,4,8,6), , cex = cex, type = 'b', lty = 3,col="darkgrey")

if(text.)## debug

text(res3d$xyz.convert(cub), labels=1:nrow(cub), col='tomato', cex=2)

}

## 6 a) The named colors in R, i.e. colors()

cc <- colors()

crgb <- t(col2rgb(cc))

par(xpd = TRUE)

rr <- scatterplot3d(crgb, color = cc, box = FALSE, angle = 24,

xlim = c(-50, 300), ylim = c(-50, 300), zlim = c(-50, 300), pch=16)

cubedraw(rr)

 

This shows how the boundaries of the cube work:

 

## 6 b) The rainbow colors from rainbow(201)

rbc <- rainbow(201)

Rrb <- t(col2rgb(rbc))

rR <- scatterplot3d(Rrb, color = rbc, box = FALSE, angle = 24,

xlim = c(-50, 300), ylim = c(-50, 300), zlim = c(-50, 300))

cubedraw(rR)

rR$points3d(Rrb, col = rbc, pch = 16)

 

Summary

 

To get to grips with R's colour functionality you are going to need to play - but that's fun! There is lots of functionality and your choice will depend on your purpose. As usual it is all about finding the best tool for the job.

Comments (1)

Anonymous said

at 11:29 am on Jun 26, 2006

Looking nice! Well done.

You don't have permission to comment on this page.