在R中切割树状图

der*_*rps 5 r dendrogram dendextend

我试图将此树形图切割成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")
Run Code Online (Sandbox Code Playgroud)

Tal*_*ili 4

您的示例不可重现,因为我们无权访问数据。我能说的是你应该看看dendextend R 包。它提供了切割树状图以及为标签和分支着色的功能。快速介绍手册显示了诸如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")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述