如何在热图中显示图例? [英] How to show legend in heatmap?

查看:586
本文介绍了如何在热图中显示图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题实际上很简单,但我无法找到解决方案.

The problem is actually quite simple, yet I am not able to find a solution to it.

如何绘制热图及其图例,即带有表示所绘制的最小值和最大值的色标的条形图?

How to plot a heatmap and its legend, i.e. a bar with the color scale representing the minimum and the maximum value that are plotted?

我阅读了 heatmap()函数的帮助,并按照下面的说明使用base R:

I read the help of the heatmap() function, and using base R as explained here:

r-graph-gallery.com热图

这就是我正在做的

heatmap(as.matrix(dataSet[, -1]), Colv = NA, Rowv = NA, scale="column", xlab="something", ylab="", main="A title", labRow=dataSet$labels, labCol=colnames(dataSet[, -1]), col= colorRampPalette(brewer.pal(8, "Oranges"))(25))

,并且效果很好,但是我仍然想绘制一个图例.有办法吗?

and it works perfectly, but still I would like to plot a legend. Is there a way to do that?

这是我正在使用的数据集的样本.第一行是标题.

this is a sample of the dataset which I'm working with. The first row is the header.

labels  6   1   4   8   3   2   9   7   5
aaa1    2   2   11  0   0   0   0   0   0
aaa2    3   3   16  0   0   0   0   0   0
aaa3    1   4   15  0   0   0   0   0   0
aaa4    1   6   17  0   0   0   0   0   4
aaa10   1   2   16  0   0   0   0   0   0
bbb11   1   0   2   0   1   2   1   0   0
bbb12   0   1   10  1   0   1   2   3   0
bbb13   1   0   0   0   2   0   0   0   0

推荐答案

您需要将legend函数添加为单独的行.

You need to add the legend function as a separate line.

library(RColorBrewer)

dataSet<-read.table(header=TRUE, text="labels  6   1   4   8   3   2   9   7   5
aaa1    2   2   11  0   0   0   0   0   0
aaa2    3   3   16  0   0   0   0   0   0
aaa3    1   4   15  0   0   0   0   0   0
aaa4    1   6   17  0   0   0   0   0   4
aaa10   1   2   16  0   0   0   0   0   0
bbb11   1   0   2   0   1   2   1   0   0
bbb12   0   1   10  1   0   1   2   3   0
bbb13   1   0   0   0   2   0   0   0   0")

heatmap(as.matrix(dataSet[, -1]), Colv = NA, Rowv = NA, 
        scale="column", xlab="something", ylab="", main="A title", 
        labRow=dataSet$labels, labCol=colnames(dataSet[, -1]), 
        col= colorRampPalette(brewer.pal(8, "Oranges"))(25))

legend(x="bottomright", legend=c("min", "ave", "max"), 
     fill=colorRampPalette(brewer.pal(8, "Oranges"))(3))

由于您要按列缩放,因此我不确定预期范围应该是多少.在上面的示例中,我在图例中假设了3个等级.为了更好地放置图例,可以调整x选项或指定x和y坐标.有关更多详细信息,请参见?legend.

Since you are scaling by the column, I am not sure what the expected range should be. In the example above, I assumed 3 levels in the legend. For better placement of the legend, you can adjust the x option or specify a x and y coordinate. See ?legend for more details.

这篇关于如何在热图中显示图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆