在R上在整个矩阵上创建热图,而不是按行

时间:2017-02-20 17:53:44

标签: r

Desired Output我想在R中创建一个热图,它应该考虑矩阵中所有值的颜色格式,而不仅仅是行。例如:我有一个以下的matix

row1 : 1,2,3,4
row2 : 50,50,50,50

当我使用heatmap()绘制此矩阵时,我会看到第1行带有颜色阴影,但我的row2标记为白色。我宁愿让50被评为极端颜色(例如:红色)和1,2,3和& 4浅色调

希望我的问题有道理。如果有任何不清楚的地方,请告诉我。 提前谢谢!

此致 Akshat

2 个答案:

答案 0 :(得分:2)

如果您仔细阅读?heatmap,则会看到默认情况下,行会按比例缩放,居中

row1 = c(1,2,3,4)
row2 = c(50,50,50,50)
m = t(as.matrix(cbind(row1, row2)))
par(mfrow=c(2,1))
heatmap(m, col=rev(heat.colors(15)))
heatmap(m, scale = "none", col=rev(heat.colors(15)))

应该做的伎俩。

enter image description here

居中和缩放(默认)

enter image description here

编辑:我颠倒了色阶,使红色映射到更大的值。

第二:您想要的输出很难获得,因为50比其他数据大得多。要重新获得第1行中的颜色详细信息,您需要在breaks函数中设置heatmap,直到获得所需的结果

heatmap(m, scale = "none", col=rev(heat.colors(5)), breaks=c(0,1,2,3,4,50))

enter image description here

答案 1 :(得分:1)

此功能应该有效

library(plotly)
hmap <- function(mat, ... ) {
s <- 1:nrow(mat)
plot_ly(x = s, y = s, z = mat[rev(s), ], type = "heatmap", ...)
}

m <- matrix(1:8, 2, byrow = T)
hmap(m)

您可以使用colors参数调整颜色,例如

hmap(m, colors = c('Red','Yellow','Green'))