Daniel Jansen Daniel Jansen - 13 days ago 6
R Question

Heatmap.2 color gradient with an additional solid color

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


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?

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.