5

我正在尝试将此树状图分为 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")
4

1 回答 1

4

从我们无权访问数据的意义上说,您的示例不可重现。我能说的是你应该看看dendextend R package。它提供了切割树状图以及为标签和分支着色的功能。快速介绍手册显示了函数的基本使用,例如labels_colorscolor_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")

在此处输入图像描述

于 2016-01-22T08:50:22.643 回答