可以使用ape
package
todrop.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)