树状图中的聚类标签
Clusters labels in dendrogram
我想知道 - 有什么方法可以将聚类标签添加到树状图中。看简单的例子:
hc = hclust(dist(mtcars))
plot(hc, hang = -1)
rect.hclust(hc, k = 3, border = "red")
所需的输出应如下所示:
感谢任何建议!
您需要获取放置集群标签的位置的坐标:
第一轴:
当您调用 rect.hclust
时,您不妨分配结果,以便您可以使用它来查找簇的开头(第一个从 1 开始,第二个从 1 开始 + 第一个的长度,等等
rh <- rect.hclust(hc, k = 3, border = "red")
beg_clus <- head(cumsum(c(1, lengths(rh))), -1)
第二轴:
你只想在红色矩形上方,它位于你有 k-1 个簇的高度和你有 k 个簇的高度的中间。假设您的目标是距离的 4/5 而不是 1/2:
y_clus <- weighted.mean(rev(hc$height)[2:3], c(4, 1))
放置标签:
text(x=beg_clus, y=y_clus, col="red", labels=LETTERS[1:3], font=2)
添加文本标签的替代方法是在处理集群标签的 mjcgraphics
包中。参见 https://github.com/drmjc/mjcgraphics and https://rdrr.io/github/drmjc/mjcgraphics/man/rect.hclust.labels.html
rect.hclust.labels(hc, k=3, border = 1 ) # adds labels to clusters
我想知道 - 有什么方法可以将聚类标签添加到树状图中。看简单的例子:
hc = hclust(dist(mtcars))
plot(hc, hang = -1)
rect.hclust(hc, k = 3, border = "red")
所需的输出应如下所示:
感谢任何建议!
您需要获取放置集群标签的位置的坐标:
第一轴:
当您调用 rect.hclust
时,您不妨分配结果,以便您可以使用它来查找簇的开头(第一个从 1 开始,第二个从 1 开始 + 第一个的长度,等等
rh <- rect.hclust(hc, k = 3, border = "red")
beg_clus <- head(cumsum(c(1, lengths(rh))), -1)
第二轴:
你只想在红色矩形上方,它位于你有 k-1 个簇的高度和你有 k 个簇的高度的中间。假设您的目标是距离的 4/5 而不是 1/2:
y_clus <- weighted.mean(rev(hc$height)[2:3], c(4, 1))
放置标签:
text(x=beg_clus, y=y_clus, col="red", labels=LETTERS[1:3], font=2)
添加文本标签的替代方法是在处理集群标签的 mjcgraphics
包中。参见 https://github.com/drmjc/mjcgraphics and https://rdrr.io/github/drmjc/mjcgraphics/man/rect.hclust.labels.html
rect.hclust.labels(hc, k=3, border = 1 ) # adds labels to clusters