【问题标题】:Cut a dendrogram剪下树状图
【发布时间】:2017-02-02 00:02:23
【问题描述】:

我有一个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

我得到的最接近的是使用apedrop.tip,但问题是如果我的depth.cutoff 包含所有叶子,如本例所示,它返回NULL

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

或者,也许我可以将dendrogram 转换为data.frame,其中还列出了每个nodedepth(包括具有depth=0 的叶子),删除所有带有@987654343 的行@&lt;depth.cutoff 从那个data.frame,然后将它转换回dendrogram

【问题讨论】:

    标签: r list dendrogram ape


    【解决方案1】:

    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)
    

    【讨论】:

    • 非常感谢@SymbolixAU。你知道是否有办法让所有分支都在同一行结束?不在情节中,而是在 dend 对象中?
    【解决方案2】:

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

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

    【讨论】:

    • 如果所有 OP 想要做的只是绘制特定部分,那肯定是一种更“方便”的方式。
    猜你喜欢
    • 2017-09-22
    • 2012-05-05
    • 2014-12-23
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-13
    相关资源
    最近更新 更多