【问题标题】:R plot upper dendrogram based on kR基于k绘制上树状图
【发布时间】:2016-01-15 20:50:35
【问题描述】:

我正在使用 hclust() 对基于 R 中 20,000 行 x 169 列数据集的距离矩阵进行聚类。当我将集群对象转换为树状图并绘制整个树状图时,即使我将其输出为相当大的 pdf,也很难阅读,因为它太大了。

df <- as.data.frame(matrix(abs(rnorm(3380000)), nrow = 20000))
mydist <- vegdist(df)
my.hc <- hclust(mydist, method = "average")
hcd <- as.dendrogram(my.hc)

pdf("hclust_plot.pdf", width = 40, height = 15)
plot(hcd)
dev.off()

我想指定要截断树状图的簇数 (k),然后仅在 k 个分割点上方绘制树状图的上部。我知道我可以根据使用函数 cut() 指定的高度 (h) 绘制上部。

pdf("hclust_plot2.pdf", width = 40, height = 15)
plot(cut(hcd, h = 0.99)$upper)
dev.off()

我也知道我可以使用 dendextend 包为具有 k 个组的树状图着色。

library(dendextend)
pdf("hclust_plot3.pdf", width = 40, height = 15)
plot(color_branches(hcd, k = 44))
dev.off()

但是对于我的数据集,这个树状图太密集了,甚至无法读取哪个组是哪个颜色。有没有一种方法可以通过指定 k 而不是 h 来仅绘制切割点上方的树状图的上部?或者有没有办法在给定 k 的情况下获得树状图的 h 值?

【问题讨论】:

标签: r cluster-analysis hierarchical-clustering dendrogram dendextend


【解决方案1】:

您可以使用the dendextend package 中的heights_per_k.dendrogram 函数来获取各种k 切割的高度。

例如:

## Not run: 
hc <- hclust(dist(USArrests[1:4,]), "ave")
dend <- as.dendrogram(hc)

library(dendextend)
dend_h <- heights_per_k.dendrogram(dend)
par(mfrow = c(1,2))
plot(dend)
plot(dend, ylim = c(dend_h["3"], dend_h["1"]))

在你的情况下:

set.seed(2016-01-16)
df <- as.data.frame(matrix(abs(rnorm(2*20000)), nrow = 20000))
mydist <- dist(df)
my.hc <- hclust(mydist, method = "average")
hcd <- as.dendrogram(my.hc)

library(dendextend)
library(dendextendRcpp)
dend_h <- heights_per_k.dendrogram(hcd) # (this can take some time)
plot(hcd, ylim = c(dend_h["43"], dend_h["1"]))

【讨论】:

  • 感谢您的帮助。我知道如何使用 color_branches() 为分支着色,但你知道我如何标记这些分支以便我可以读取哪个组是哪个组吗?
  • 在 color_branches 中使用“groupLabels = TRUE”参数
  • 感谢@Tal 当我尝试 'd1
猜你喜欢
  • 2015-03-17
  • 2018-06-18
  • 2015-07-05
  • 2015-05-21
  • 1970-01-01
  • 1970-01-01
  • 2020-06-06
  • 2020-11-19
  • 2020-04-05
相关资源
最近更新 更多