我试图将这个树形图切割成3组:(T24,T1,T17等),(T12,T15,T6等)和(T2,T8,T3,T9)
我尝试过使用cutree(hc,k = 3,h = 400),但它继续制作相同的组。任何帮助是极大的赞赏。这是我的代码。
#temps must have date/time as column headers, not row headers
load(temps)
distMatrix <- dist(temps)
#create label colors
labelColors = c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#FFFF33")
# cut dendrogram in 3 clusters
clusMember = cutree(hc, k=3, h=400)
colLab <- function(n) {
if (is.leaf(n)) {
a <- attributes(n)
labCol <- labelColors[clusMember[which(names(clusMember) == a$label)]]
attr(n, "nodePar") <- c(a$nodePar, lab.col = labCol)
}
n
}
hcd = as.dendrogram(hc)
clusDendro = dendrapply(hcd, colLab)
plot(clusDendro, main = "Cluster Analysis")
答案 0 :(得分:2)
在我们无法访问数据的意义上,您的示例无法重现。
我可以说你应该看看dendextend R package。它提供了剪切树状图的功能,以及着色标签和分支。 Quick Introduction手册显示了labels_colors
和color_branches
等函数的基本用法,用于生成如下图:
在您的情况下,由于您的分支似乎处于同一高度,因此您不太可能直接控制其削减。你可以做的是使用branches_attr_by_clusters
来专门控制你关心的子集群的颜色。这是一个例子:
x <- c(1:3, 6:8)
dend <- as.dendrogram(hclust(dist(x), method = "ave"))
library(dendextend)
labels(dend) <- x[order.dendrogram(dend)]
# due to the ties - there is specific reason to have this be these 3 clusters:
cutree(dend, k = 3)[order.dendrogram(dend)]
par(mfrow = c(1,2))
dend1 <- color_branches(dend, k = 3)
dend1 <- color_labels(dend1, k = 3)
plot(dend1, main = "default cuts by cutree")
# let's force it to be another 3 clusters:
dend2 <- branches_attr_by_clusters(dend, c(1, 2,2, 3,3,3), c("gold", "darkgreen", "blue"))
# coloring the labels is actually the easiest part:
labels_colors(dend2) <- c("gold", "darkgreen", "blue")[c(1, 2,2, 3,3,3)]
plot(dend2, main = "Manual cuts")