【发布时间】:2018-12-20 15:45:20
【问题描述】:
可重现的数据:
data(crabs, package = "MASS")
df <- crabs[-(1:3)]
set.seed(12345)
df$GRP <- kmeans(df, 4)$cluster
df.order <- dplyr::arrange(df, GRP)
数据说明:
df 有 5 个数值变量。我根据这 5 个属性做了 K-means 算法,生成了一个新的分类变量GRP,它有 4 个级别。接下来,我用GRP 订购并命名为df.order。
我对 pheatmap 做了什么:
## 5 numerical variables for coloring
colormat <- df.order[c("FL", "RW", "CL", "CW", "BD")]
## Specify the annotation variable `GRP` shown on left side of the heatmap
ann_row <- df.order["GRP"]
## gap indices
gapRow <- cumsum(table(ann_row$GRP))
library(pheatmap)
pheatmap(colormat, cluster_rows = F, show_rownames = F,
annotation_row = ann_row, gaps_row = gapRow)
annotation_colors[[colnames(annotation)[i]]] 中的错误: 下标越界
这是我发现奇怪的地方:
一开始我猜是annotation_row这个参数引起的。我检查了两个数据框的行名。
all.equal(rownames(colormat), rownames(ann_row))
# [1] TRUE
你可以看到他们是平等的。但是,我执行了以下代码并且热图工作。
rownames(colormat) <- rownames(ann_row)
pheatmap(colormat, cluster_rows = F, show_rownames = F,
annotation_row = ann_row, gaps_row = gapRow)
理论上这段代码"rownames(colormat) <- rownames(ann_row)"应该没有意义,因为这两个对象本来是相等的,但是为什么它会让pheatmap()函数起作用呢?
编辑:根据@steveb 的评论,我什至不必使用ann_row 设置行名。我刚刚设置了
rownames(colormat) <- rownames(colormat)
并且 pheatmap 也可以使用。这种情况仍然违反直觉。
最终输出:
【问题讨论】:
-
我不知道答案,但你的问题应该出现在 R-reproducible Hall of Fame。写的很好。
-
我怀疑如果您执行以下
rownames(colormat) <- rownames(colormat),那么pheatmap将起作用;您甚至不必使用ann_row设置rownames。