Re: [R] rainbow function

From: Barry Rowlingson <>
Date: Mon, 07 Jan 2008 11:05:35 +0000

Wang, Zhaoming (NIH/NCI) [C] wrote:
> Hello
> I'm using rainbow function to generate 10 colors for the plot and it is
> difficult to tell the neighboring colors from each other. How can I make
> the colors more differently.

  If all you want is for neighbouring colours to be distinguishable you could just sample(rainbow(10)) until you get a palette with neighbouringly distinguishable colours:

# some neighbours look similar:
plot(1:10, col=rainbow(10))

# so try this:
set.seed(1) # so your random numbers are the same as mine...

p = sample(rainbow(10))
plot(1:10, col=p)

# hmmm green and cyan are still next to each other. Lets try again:


This is fine for simple plots where coloured objects are discrete, such as in a bar chart, but perhaps wouldn't work for overlapping coloured objects, like several time series together or several scatterplots. Perhaps use 5 colours and two point types for points, or two line styles for lines, or two fill styles for areas.

rainbow() is great - for drawing rainbows - but the palettes from the RColorBrewer package are much better for statistical plots as someone else suggested. When I write code for plots I try and use RColorBrewer if it's there:

getColours <- function(nv=4){


     warning("This plot would like nicer if you installed RColorBrewer")


Barry mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Mon 07 Jan 2008 - 11:16:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 07 Jan 2008 - 12:30:05 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive