R: Why Heatmap shows white color for rows with equal number?

五迷三道 提交于 2019-12-11 00:17:00

问题


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")

Thank you so much for helping


回答1:


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.




回答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.



来源:https://stackoverflow.com/questions/19639575/r-why-heatmap-shows-white-color-for-rows-with-equal-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!