Skip to content

heatmap.2 color range

5 messages · Dieter Menne, evgeny55

#
Hi,
I'm trying to create a heatmap with color ranges for different values in my
matrix. For example:
If  x > 5 , use orange gradient
if  x < 1.5, use red gradient
.....

Right now I have the following:
orgPal<-brewer.pal(3,"Oranges")
bluPal<-brewer.pal(3,"Blues")
redPal<-brewer.pal(3,"Reds")

grad <- ifelse(randMat > 5,orgPal,ifelse(randMat<1.5,redPal,bluPal))
hmap <- heatmap(randMat,Rowv=NULL, Colv=NULL,dendrogram="none",col = grad,
scale="column",...)

the problem is that all the colors become very close to white on one end of
the spectrum and the orange colors, for example, start approaching red when
x reaches extreme values and the heatmap loses its effect.  Is there an easy
way to specify a start and end range for each of my colors so that the hue
does not change dramatically within each spectrum.  I'm sure that I can
explicitly specify all the colors within my ifelse statement but this is
rather time consuming.

thanks,
John
#
as a followup, I tried using the rainbow function to create the gradients but
is there a way to do a "reverse" rainbow, ie. normally if I do: 

pie(rep(1,6), col=rainbow(6,start=0, end=.07))

I'll get a gradient from dark red to orangish but what if I want it to go
the other way

thanks
#
evgeny55 wrote:
I often use larger palettes (brewer.pal(7,...)) and remove the middle range.

Dieter
#
thanks, 
I think I got the color ranges down, however, I just realized that the
colors don't match the data.  When I execute:
grad <- ifelse(randMat > 5,yelPal,ifelse(randMat<1.5,redPal,bluPal))    
the grad matrix contains the correct hex codes corresponding to the randMat
data matrix  but when I run:

heatmap(randMat, Rowv=NA, Colv=NA, col = grad, scale="none",
margins=c(5,10))

The colors displayed don't match up.  I'm not sure if it's re-ordering the
data somehow but I'm not getting any warning or errors and can't find any
similiar postings.