[R] Heatmap Colors

David Winsemius dwinsemius at comcast.net
Sat Sep 29 01:52:41 CEST 2012


On Sep 28, 2012, at 3:16 PM, Nick Fankhauser wrote:

> Hello R-Users!
> 
> I'm using a heatmap to visualize a matrix of values between -1 and 3.
> How can I set the colors so that white is zero, below zero is blue of increasing intensity towards -1 and above zero is red of increasing intensity towards red?
> 
> I tried like this (using the marray and gplots packages from bioconductor):
> mcol <- maPalette(low="blue", mid="white", high="red",k=100)
> heatmap.2(my_matrix, col=mcol)
> 
> But white does not correspond to zero, because the value distribution is not symmetrical, so that zero is not in the middle.
> Is it somehow possible to create a color palette with white centered at zero?

The way you stated it at the beginning, I thought you should want the palette centered at 1 rather than 0:

test <- seq(-1,3, len=20)
shift.BR <- colorRamp(c("blue","white", "red"), bias=2)((1:16)/16)
tpal <- rgb(shift.BR, maxColorValue=255)
barplot(test,col = tpal)

Perhaps I was being led astray by a somewhat similar question on StackOverflow.

-- 
David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list