如何在热图中显示图例?

时间:2019-08-07 13:24:49

标签: r heatmap

问题实际上很简单,但我找不到解决方案。

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

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

r-graph-gallery.com heatmaps

这就是我正在做的

{
"Meta Data": {
    "1. Information": "Daily Time Series with Splits and Dividend Events",
    "2. Symbol": "SMT.LON",
    "3. Last Refreshed": "2019-08-07",
    "4. Output Size": "Compact",
    "5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
    "2019-08-07": {
        "1. open": "521.0000",
        "2. high": "530.0000",
        "3. low": "519.5000",
        "4. close": "522.1314",
        "5. adjusted close": "522.1314",
        "6. volume": "1259995",
        "7. dividend amount": "0.0000",
        "8. split coefficient": "1.0000"
    },
    "2019-08-06": {
        "1. open": "516.5000",
        "2. high": "525.9450",
        "3. low": "513.0000",
        "4. close": "518.0000",
        "5. adjusted close": "518.0000",
        "6. volume": "3353161",
        "7. dividend amount": "0.0000",
        "8. split coefficient": "1.0000"
    },
    "2019-08-05": {
        "1. open": "5.2430",
        "2. high": "5.2430",
        "3. low": "5.2430",
        "4. close": "5.2430",
        "5. adjusted close": "5.2430",
        "6. volume": "0",
        "7. dividend amount": "0.0000",
        "8. split coefficient": "1.0000"
    },
    "2019-08-02": {
        "1. open": "5.3989",
        "2. high": "5.3989",
        "3. low": "5.3989",
        "4. close": "5.3989",
        "5. adjusted close": "5.3989",
        "6. volume": "0",
        "7. dividend amount": "0.0000",
        "8. split coefficient": "1.0000"
    },
    "2019-08-01": {
        "1. open": "558.0000",
        "2. high": "568.5000",
        "3. low": "557.0000",
        "4. close": "568.5000",
        "5. adjusted close": "568.5000",
        "6. volume": "2141407",
        "7. dividend amount": "0.0000",
        "8. split coefficient": "1.0000"
    },
    "2019-07-31": {
        "1. open": "562.5000",
        "2. high": "565.5000",
        "3. low": "560.0580",
        "4. close": "563.0000",
        "5. adjusted close": "563.0000",
        "6. volume": "1519041",
        "7. dividend amount": "0.0000",
        "8. split coefficient": "1.0000"
    },
    "2019-07-30": {
        "1. open": "5.5441",
        "2. high": "5.5441",
        "3. low": "5.5441",
        "4. close": "5.5441",
        "5. adjusted close": "5.5441",
        "6. volume": "0",
        "7. dividend amount": "0.0000",
        "8. split coefficient": "1.0000"
    },
    "2019-07-29": {
        "1. open": "5.5449",
        "2. high": "5.5449",
        "3. low": "5.5449",
        "4. close": "5.5449",
        "5. adjusted close": "5.5449",
        "6. volume": "0",
        "7. dividend amount": "0.0000",
        "8. split coefficient": "1.0000"
    },

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

这是我正在使用的数据集的示例。第一行是标题。

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))

1 个答案:

答案 0 :(得分:1)

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

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="topleft", legend=c("min", "ave", "max"), 
     fill=colorRampPalette(brewer.pal(8, "Oranges"))(3))

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