热图颜色键有五种不同的颜色

时间:2012-07-29 22:32:10

标签: r heatmap

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

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)

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

詹姆斯

1 个答案:

答案 0 :(得分: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"))
相关问题