可重现的数据:
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 也有效。这种情况仍然违反直觉。
最终输出: