2

我想折叠dendrogram给定公差截止的分支。

我正在遵循dendextendcollapse_branch 例子

require(dendextend)
dend <- iris[1:5,-5] %>% dist %>% hclust %>% as.dendrogram 
dend %>% ladderize %>% plot(horiz = TRUE); abline(v = .2, col = 2, lty = 2)

在此处输入图像描述

示例dendrogramdendextend的 s不同,我想用三角形替换所有折叠的分支(即垂直红色虚线的任何进化枝),类似于图中进化枝的呈现方式(来自此链接)

在此处输入图像描述

如果这要求太多,我会满足于在公差截止处切割树枝。

4

2 回答 2

2

获得三角形确实有点太多了,但你可以给树枝上色。按高度或簇数,使用color_branches

library(dendextend)
dend <- iris[1:5,-5] %>% dist %>% hclust %>% as.dendrogram 
dend %>% color_branches(h=0.2) %>% ladderize %>% plot(horiz = TRUE); abline(v = .2, col = 2, lty = 2)
# OR
# dend %>% color_branches(k=4) %>% ladderize %>% plot(horiz = TRUE); abline(v = .2, col = 2, lty = 2)

在此处输入图像描述

您还可以选择find_k使用轮廓系数的聚类数(在本例中为 2):

require(dendextend)
dend <- iris[1:5,-5] %>% dist %>% hclust %>% as.dendrogram 
find_k(dend)$k
dend %>% color_branches(k=find_k(.)$k) %>% ladderize %>% plot(horiz = TRUE); abline(v = .2, col = 2, lty = 2)

在此处输入图像描述

于 2017-01-28T11:06:58.593 回答
1

可以使用ape packagetodrop.tip的:

require(ape)
require(dendextend)
require(data.tree)

dend <- iris[1:5,-5] %>% dist %>% hclust %>% as.dendrogram 
tol.level <- 0.28
dend %>% plot(horiz = TRUE); abline(v=tol.level,col="red",lty=2)

在此处输入图像描述

所以我们的容忍度是 0.28,因此我们想要折叠叶子(1,5)(3,4)因为它们的祖先节点的深度低于tol.level

#convert dendrogram to data.tree
dend.dt <- as.Node(dend)

#get vector of leaves per each internal node
node.list <- lapply(dend.dt$Get(function(node) node$leaves,filterFun = isNotLeaf),function(n) unname(sapply(unlist(n,recursive = T),function(l) l$name)))
#get vector of per each internal node
node.depth.df <- data.frame(depth=c(t(sapply(Traverse(dend.dt,traversal="pre-order",pruneFun=isNotLeaf),function(x) c(x$plotHeight)))),stringsAsFactors=F)

to.drop.leave.names <- c(sapply(which(node.depth.df$depth < tol.level),function(i) node.list[[i]]))

#convert dendrogram to phylo
phylo.dend <- as.phylo(dend)
phylo.dend <- drop.tip(phylo.dend,tip=to.drop.leave.names,interactive=FALSE,trim.internal=FALSE)
plot(phylo.dend,use.edge.length=F)

在此处输入图像描述

现在我们可以将其转换回dendrogram( Chronogram)

new.dend <- chronos(phylo.dend)
于 2017-01-31T00:00:00.783 回答