Heatmap.2 color gradient with an additional solid color

1.1k views Asked by At

The heatmap I am using creates a gradient from 0.7 to 1.3 using heatmap.2:

heatmap.2(lifespan.matrix, col=bluered, breaks=c(seq(0.7,1.3,0.01)),
Rowv = FALSE, Colv = FALSE, trace="none", main="Lifespan")

In the heatmap you can see the gradient emerge ending oftenly in a solid blue line. This happens because of zero values in the matrix at these points.

enter image description here

I'd like to change this color, in the matrix noted as 0, into a different color i.e. yellow. Could anyone help me with this problem?

2

There are 2 answers

0
Nightwriter On BEST ANSWER
breaks <- seq(0.7,1.3,0.01)
lifespan.matrix <- matrix(sample(c(breaks,rep(0,100)),100,replace=TRUE),nrow=10)
heatmap.2(lifespan.matrix, col=c("#FFFF00",bluered(length(breaks)-2)), breaks=breaks, Rowv = FALSE, Colv = FALSE, trace="none", main="Lifespan")

You specify the exact colors associated with the breaks.

0
Joe On

An easy and effective workaround is to replace your zero values with NAs in the matrix and colour them separately with the na.color argument of heatmap.2. See my answer to this question: https://stackoverflow.com/a/40031933/4477364