[R] Attempting to make a custom color spectrum to use in heatmap.2

Jim Lemon jim at bitwrit.com.au
Thu Aug 7 11:18:53 CEST 2008


On Wed, 2008-08-06 at 16:57 -0700, Daniel Bernstein wrote:
> Hello there! I'd just like to say in advance, "Thank you," for any help 
> and/or advice.
> 
> My problem is as follows:
> 
> I have a dataset that is made up of percentages. I've assigned my 
> "error" percentages a value of '-100', my "non-existent" percentages a 
> value of '0', and all my other percentages are normal values that range 
> from the high 60's to 100. I would like to create a heatmap that 
> designates my "error" values as gray, my "non-existent" values as black, 
> and I would like to to have the rest of my values, say 50 to 100, as a 
> rainbow-type spectrum (like the palette "Spectral" in RColorBrewer, 
> except with 50 values).
> 
> I've tried using breaks, and then implementing the breaks in my 
> heatmap.2 command. The breaks work just fine. I guess what I want to 
> control is the range of the:
> 
> col=(colorpanel(#, low="color1", mid="color2", high="color3"))
> 
> command. Is there any way to set more values than "low," "mid," and 
> "high?" If that is possible I think it would solve my problem. I've 
> looked over the documentation and searched over previous 
> color/heatmap-related questions, but haven't come across anything that 
> points me in the right direction.
> 

Hi Daniel,
You can map values to colors using "color.scale" in the plotrix package.
What you want would require that you separate out the 50+ values to get
the graduated colors, then set the -100 and 0 values to black and gray.
Say your dataset is a matrix named "mydata", a sneaky way might be:

missval<-which(mydata == -100)
nonext<-which(mydata == 0)
# this gives you the lowest value that you want in the color range
mydata[c(missval,nonext)]<-50
# this gives you a matrix of colors that start at 50=red and
# range to 100=blue
mycellcolors<-color.scale(mydata,c(1,1,0,0),c(0,1,1,0),c(0,0,0.5,1))
# the next two lines set the colors for missing and non-existent values
mycellcolors[missval]<-"black"
mycellcolors[nonext]<-"gray"
# display the matrix as colors, using the calculated colors
color2D.matplot(mydata,cellcolors=mycellcolors)

I can't work out how to do this with "image" or "heatmap", but someone
else might be able to tell you.

Jim



More information about the R-help mailing list