R 树状图父子集群

R Dendrogram Parent-Child Clusters

我正在尝试找出如何检索哪些集群是 "children/descendent" 到 "parent" 集群。让我用下面的情节来说明这一点。

此图是具有不同聚类解决方案的正常树状图。我想画的是较小的集群和较大的集群之间的路径。我想这样做的原因是我有一个非常大的数据集并且我有复杂的集群,我需要了解哪些集群 "descend"(小集群)来自大集群。

# Load data
data(USArrests)

# Compute distances and hierarchical clustering
dd <- dist(scale(USArrests), method = "euclidean")
hc <- hclust(dd, method = "ward.D2")

par(mfrow = c(2,2))
# Plot the obtained dendrogram
plot(hc, cex = 0.6, hang = -1)
rect.hclust(hc, k = 2, border = 2:5)

plot(hc, cex = 0.6, hang = -1)
rect.hclust(hc, k = 4, border = 2:5)

plot(hc, cex = 0.6, hang = -1)
rect.hclust(hc, k = 8, border = 2:5)

plot(hc, cex = 0.6, hang = -1)
rect.hclust(hc, k = 12, border = 2:5)

比如我这里有两个方案:2簇和4簇。我不清楚我如何知道从 2 sub_grp1 个集群(依此类推)中划分出哪些 sub_grp2 个集群。

# Cut tree into 4 groups
sub_grp1 <- cutree(hc, k = 2)
sub_grp2 <- cutree(hc, k = 4)
sub_grp3 <- cutree(hc, k = 8)
sub_grp4 <- cutree(hc, k = 12)

USArrests$sub_grp1 = sub_grp1
USArrests$sub_grp2 = sub_grp2
USArrests$sub_grp3 = sub_grp3
USArrests$sub_grp4 = sub_grp4

我真正想画的或以任何方式检索的东西是这样的:

这真的可以帮助我从较大的集群中了解哪些较小的集群 "descend"。

这有意义吗?

您可以尝试 clustree 包。顺序可能与树状图中的顺序不相似,但您可以看到关系:

library(clustree)
data(USArrests)

# Compute distances and hierarchical clustering
dd <- dist(scale(USArrests), method = "euclidean")
hc <- hclust(dd, method = "ward.D2")

Ks = c(1,2,4,6,8)
clus_results = sapply(Ks,function(i){
cutree(hc,i)
})

colnames(clus_results) = paste0("K",Ks)
clustree(clus_results, prefix = "K")

一种解决方案是将树状图转换为 igraph 图并使用 igraph 中提供的绘图工具。

所有 50 个州有点拥挤,但您可以看到树结构。

## Convert to a phylo,  then to igraph
library(ape)
PH = as.phylo(hc)
IG = as.igraph(PH)

## Make a nice layout
LO = layout_as_tree(IG)
LO2 = LO[,2:1]
LO2[,1] = LO2[,1]*6

## plot
plot(IG, layout=LO2, vertex.size=80, edge.arrow.size=0.5,
    rescale=F, vertex.label.cex = 0.8,
    xlim=range(LO2[,1]), ylim=range(LO2[,2]))