剪一个树状图

时间:2017-02-02 00:02:23

标签: r list dendrogram ape

我有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)

enter image description here

最后得到dendrogram

enter image description here

我最接近的是使用ape的{​​{1}},但问题是如果我的drop.tip包含所有叶子,就像在此示例中一样,它返回{{1} }。

也许有人知道如果他们的depth.cutoff低于NULL,我是否以及如何删除代表nested list的{​​{1}}中的元素?

或者,也许我可以将dendrogram转换为depth,其中还会列出每个depth.cutoff的{​​{1}}(包括将有dendrogram的树叶= 0),从data.frame移除depth node depth的所有行,然后将其转换回depth

2 个答案:

答案 0 :(得分:2)

cut将在指定高度剪切树。它将返回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)

enter image description here

答案 1 :(得分:2)

获得图片的一种更直接的方法就是设置您想要绘制的限制。

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

Cropped Dendrogram