【问题标题】:Heatmap color key with five different colors具有五种不同颜色的热图颜色键
【发布时间】:2012-07-29 22:32:09
【问题描述】:

我有以下代码,但不知道如何使用它来显示带有颜色键的热图,该颜色键显示代表定义值的五种不同颜色:

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)

如果有人可以提供帮助,我会很高兴。谢谢!

詹姆斯

【问题讨论】:

  • 是 R。我不确定他是否可以与 rep 1 一起编辑或评论...
  • @JohanLundberg 我认为这些代表限制不适用于您自己的问题(或您自己问题的答案)。
  • 顺便说一句,我不认识heatmaps.2 函数,所以我在RSeek 上搜索了它。 first hit 可能会回答你的问题。
  • @joran,我认为对于 cmets 他们是...
  • @JohanLundberg 不,他们dont't。

标签: r heatmap


【解决方案1】:
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。那是完美的解决方案。 legend() 非常适合我的需要。非常感谢您的帮助!!!
猜你喜欢
  • 2019-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-16
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
相关资源
最近更新 更多