3

in the following example, a matrix of 3 cols and 5 rows. when I create a heatmap, any row that has similar numbers like the example below (0.3,0.3,0.3) it shows white color in the heatmap. and my intention is to change it to any desirable color. Example: here is an example:

    A = matrix(c(0.0183207, 0.0000000, 0.1468750, 0.03, 0.03, 0.03,0.4544720,0.0000000,0.1395850,0.002,0,0,1.1,1,1),nrow=5,ncol=3,byrow = TRUE) 
dimnames(A) = list(c("row1", "row2","row3","row4","row5"),c("col1", "col2", "col3"))
heatmap.2( A,col =redgreen, scale = "row", cexRow=0.3, cexCol=0.8, margins=c(6,6), trace="none")

In the example, we see a row is white color which came from the row data (0.3,0.3,0.3)

Thank you so much for helping

jesy2013
  • 319
  • 2
  • 12
  • 2
    Maybe you could give us some data with `dput`? Or make it [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – Gregor Thomas Oct 28 '13 at 16:18
  • @shujaa, I also uploaded the screenshot of the input data. hope that would help. – jesy2013 Oct 28 '13 at 18:50
  • No, it really doesn't. I want to try fiddling with your commands on my computer. I'm not going to manually type in your data from the screenshot. If you type `dput(TheData)` into R, then copy and paste the result into your question, then I can copy and paste the output into my R session and run your commands. – Gregor Thomas Oct 28 '13 at 19:22
  • @shujaa I think now it is better. – jesy2013 Oct 28 '13 at 21:55

2 Answers2

4

The color is white because your heatmap command scales the rows before drawing the heatmap. So the row c(0.3, 0.3, 0.3) becomes a row of zeroes and zero is denoted by white in this color scheme.

If you want some other color scheme for these rows you must either think if you really want to scale the rows or play with the breaks and col arguments to create separate color for value 0.

Raivo Kolde
  • 729
  • 6
  • 14
  • thanks. it kind of gives a solution but unfortunately doesn't doesn't work if I want to use "redgreen(60)". – jesy2013 Oct 29 '13 at 13:32
2

What happens when you scale a constant vector? You're telling R to divide by 0, which gets you NaN.

scale(c(0.3, 0.3, 0.3))

This is what's happening when you tell heatmap.2 to scale by row, but one of your rows has no variation. And since NaN isn't a number, it's plotted as white. If you want to color it black instead, then I think you should scale your data manually beforehand, and replace the NaN's with 0.

scaled_A <- t(apply(A, MARGIN=1, FUN=scale))
scaled_A[is.nan(scaled_A)] <- 0

Then you can do the heatmap call

heatmap.2( scaled_A,col =redgreen,
           scale = "none",
           cexRow=0.3, cexCol=0.8,
           margins=c(6,6), trace="none",
           dendrogram='none')

And it's black. It seems to switch the order as is, but you can probably figure out how to fix that.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • thanks for that. The point is, I don't wanna remove those rows which has no variation (NaN), because I want to be told that such row has no variation that is why I want to still have them in my data. somewhere I have seen matrix like that but don't know how they made it. – jesy2013 Nov 04 '13 at 10:12
  • @jesy2013 Your original question meets those requirements. A row of white means "no variation". – Gregor Thomas Nov 04 '13 at 16:47