我有dendrogram
:
set.seed(10)
mat <- matrix(rnorm(20*10),nrow=20,ncol=10)
dend <- as.dendrogram(hclust(dist(mat)))
并给出深度截止值:
我想切断那个临界点右边的所有分支。
depth.cutoff <- 4.75
我想剪掉虚线右边的所有分支:
plot(dend,horiz = TRUE)
abline(v=depth.cutoff,col="red",lty=2)
最后得到dendrogram
:
我最接近的是使用ape
的{{1}},但问题是如果我的drop.tip
包含所有叶子,就像在此示例中一样,它返回{{1} }。
也许有人知道如果他们的depth.cutoff
低于NULL
,我是否以及如何删除代表nested list
的{{1}}中的元素?
或者,也许我可以将dendrogram
转换为depth
,其中还会列出每个depth.cutoff
的{{1}}(包括将有dendrogram
的树叶= 0),从data.frame
移除depth
node
depth
的所有行,然后将其转换回depth
?
答案 0 :(得分:2)
cut
将在指定高度剪切树。它将返回upper
和lower
部分的列表
cut(dend, h = depth.cutoff)$upper
# $upper
# 'dendrogram' with 2 branches and 5 members total, at height 5.887262
#
# $lower
# $lower[[1]]
# 'dendrogram' with 2 branches and 6 members total, at height 4.515119
#
# $lower[[2]]
# 'dendrogram' with 2 branches and 2 members total, at height 3.789259
#
# $lower[[3]]
# 'dendrogram' with 2 branches and 5 members total, at height 3.837733
#
# $lower[[4]]
# 'dendrogram' with 2 branches and 3 members total, at height 3.845031
#
# $lower[[5]]
# 'dendrogram' with 2 branches and 4 members total, at height 4.298743
plot(cut(dend, h = depth.cutoff)$upper, horiz = T)
答案 1 :(得分:2)