有没有一种简单的方法可以在根处合并多个 hclust 对象(或树状图)?
我已经使示例尽可能完整以说明我的问题。
假设我想按地区对 USArrests 进行聚类,然后将所有 hclust 对象联合起来,在热图中将它们绘制在一起。
USArrests
Northeast <- c("Connecticut", "Maine", "Massachusetts", "New Hampshire", "Rhode Island",
"Vermont", "New Jersey", "New York", "Pennsylvania")
Midwest <- c("Illinois", "Indiana", "Michigan", "Ohio", "Wisconsin",
"Iowa", "Kansas", "Minnesota", "Missouri", "Nebraska", "North Dakota",
"South Dakota")
South <- c("Delaware", "Florida", "Georgia", "Maryland", "North Carolina",
"South Carolina", "Virginia", "West Virginia",
"Alabama", "Kentucky", "Mississippi", "Tennessee", "Arkansas",
"Louisiana", "Oklahoma", "Texas")
West <- c("Arizona", "Colorado", "Idaho", "Montana", "Nevada", "New Mexico",
"Utah", "Wyoming", "Alaska", "California", "Hawaii", "Oregon", "Washington")
h1 <- hclust(dist(USArrests[Northeast,]))
h2 <- hclust(dist(USArrests[Midwest,]))
h3 <- hclust(dist(USArrests[South,]))
h4 <- hclust(dist(USArrests[West,]))
现在我有 4 个 hclust 对象(h1 到 h4)。我通常像这样合并它们:
hc <- as.hclust(merge(merge(merge(
as.dendrogram(h1), as.dendrogram(h2)), as.dendrogram(h3)),
as.dendrogram(h4)))
然后,要绘制它们,我必须根据 hclust 对象重新排序矩阵,然后绘制(我添加了一些注释以使绘图更清晰):
usarr <- USArrests[c(Northeast, Midwest, South, West),]
region_annotation <- data.frame(Region = c(rep("Northeast", length(Northeast)),
rep("Midwest", length(Midwest)),
rep("South", length(South)),
rep("West", length(West))),
row.names = c(Northeast, Midwest, South, West))
pheatmap(usarr, cluster_rows = hc,
annotation_row = region_annotation)
总而言之:有没有比合并所有单独的 hclust 更简单的方法?