热力图颜色键,包括五种不同的颜色。

3
我有以下代码,不确定如何使用它来显示一张热力图,并使用五种不同的颜色表示定义值的颜色键:
hm <- heatmap.2(data_matrix, scale="none",Rowv=NA,Colv=NA,col = rev(brewer.pal(11,"RdBu")),margins=c(5,5),cexRow=0.5, cexCol=1.0,key=TRUE,keysize=1.5, trace="none")

需要颜色关键字:

<0.3 (blue)

0.3-1 (green)

1-1.3 (yellow)

1.3-3.0 (orange)

>3.0 (red)

我很乐意为您提供帮助。谢谢! James

这是关于 R 的事情。我不确定他是否有权限使用声望值为1进行编辑或评论。 - Johan Lundberg
@JohanLundberg 我不相信那些声望限制适用于您自己的问题(或您自己问题上的答案)。 - joran
顺便提一下,我不认识heatmaps.2函数,所以我在RSeek上搜索了一下。第一个链接可能已经回答了你的问题。 - Gregor Thomas
@joran,我认为对于注释,他们确实... - Johan Lundberg
@JohanLundberg 不,他们不会。 - joran
1个回答

8
require(gplots)
require(RColorBrewer)

## Some fake data for you
data_matrix <- matrix(runif(100, 0, 3.5), 10, 10)

## The colors you specified.
myCol <- c("blue", "green", "yellow", "orange", "red")
## Defining breaks for the color scale
myBreaks <- c(0, .3, 1, 1.3, 3, 3.5)

hm <- heatmap.2(data_matrix, scale="none", Rowv=NA, Colv=NA,
                col = myCol, ## using your colors
                breaks = myBreaks, ## using your breaks
                dendrogram = "none",  ## to suppress warnings
                margins=c(5,5), cexRow=0.5, cexCol=1.0, key=TRUE, keysize=1.5,
                trace="none")

这应该可以工作,并给您一些想进一步编辑的思路。如果您想得到具有您精确值的图例,则不要使用内置的直方图,而是使用legend

hm <- heatmap.2(data_matrix, scale="none", Rowv=NA, Colv=NA,
                col = myCol, ## using your colors
                breaks = myBreaks, ## using your breaks
                dendrogram = "none",  ## to suppress warnings
                margins=c(5,5), cexRow=0.5, cexCol=1.0, key=FALSE,
                trace="none")
legend("left", fill = myCol,
    legend = c("0 to .3", "0.3 to 1", "1 to 1.3", "1.3 to 3", ">3"))

感谢您的帮助,shujaa。不知何故,颜色键中的颜色与预期的值不匹配;黄色在颜色键中根本没有出现。 - James Johnson
非常感谢你,Shujaa。那是完美的解决方案。legend() 函数非常符合我的需求。非常感谢你的帮助!!! - James Johnson

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接