简体   繁体   English

切割树状图

[英]Cut a dendrogram

I have a dendrogram : 我有一个dendrogram

set.seed(10)
mat <- matrix(rnorm(20*10),nrow=20,ncol=10)
dend <- as.dendrogram(hclust(dist(mat)))

And given a depth cutoff: 并给出深度截止:

I'd like to cut all branches that are to the right to that cutoff. 我想剪切掉该截止点右边的所有分支。

depth.cutoff <- 4.75

I'd like to cut all branches to the right of the dashed line: 我想在虚线右边剪切所有分支:

plot(dend,horiz = TRUE)
abline(v=depth.cutoff,col="red",lty=2)

在此处输入图片说明

And to end up with this dendrogram : 最后是这个dendrogram

在此处输入图片说明

The closest I got was using ape 's drop.tip , but the problem with that is if my depth.cutoff includes all leaves, as in this example, it returns NULL . 我得到的最接近的是使用apedrop.tip ,但是问题是如果我的depth.cutoff包括所有叶子,如本例所示,它返回NULL

Perhaps anyone knows if and how I can delete elements from the nested list which represents my dendrogram if their depth is below depth.cutoff ? 也许有人知道,如果depth超过depth.cutoff我是否以及如何从代表我的dendrogramnested list删除元素?

Alternatively, perhaps I can convert the dendrogram to a data.frame , which also lists the depth of each node (including leaves which will have depth =0), remove all rows with depth < depth.cutoff from that data.frame , and then convert that back to a dendrogram ? 或者,也许我可以将dendrogram转换为data.frame ,它也列出每个nodedepth (包括depth = 0的叶子),从该data.frame删除depth < depth.cutoff所有行,然后转换回dendrogram

cut will cut the tree at a specified height. cut将在指定的高度切割树。 It will return a list of the upper and lower portions 它将返回upperlower的列表

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)

在此处输入图片说明

A perhaps more direct way of getting your picture is just to set the limits that you want to plot. 一种获取图片的直接方法可能只是设置要绘制的界限。

plot(dend, horiz = TRUE, xlim=c(6,4.75))

裁剪树状图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM