【问题标题】:manually create a dendrogram r手动创建树状图 r
【发布时间】:2019-09-09 00:31:57
【问题描述】:

我正在尝试根据我不是通过 hclust 或任何其他方式获得的相似性分数来创建树状图。我有两个分支,只是想根据它们的相似程度将它们画出来,然后将它们分支出来。

A 和 B 相似度为 0.5 A 是 0.2 唯一的 B 是 0.3 唯一

所以 A 的总高度是 0.7,B 的总高度是 0.8,它们的 0.5 个分支是共享的。

下面只是做了两个分支,没有连接两个叶子的长分支。有this similar question,但是用处不大!

x <- list(1, 2)
## attach "leaf" and "label" attributes to leaf nodes
attr(x[[1]], "leaf") <- TRUE
attr(x[[2]], "leaf") <- TRUE
attr(x[[1]], "label") <- "A"
attr(x[[2]], "label") <- "B"

## set "height" attributes for all nodes
attr(x, "height") <- 1
attr(x[[1]], "height") <- (1-0.7)
attr(x[[2]], "height") <- (1-0.8)

## set "midpoints" attributes for all nodes
attr(x, "midpoint") <- 1
attr(x[[1]], "midpoint") <- 0.5
attr(x[[2]], "midpoint") <- 0.5

## set "members" attributes for all nodes
attr(x, "members") <- 2
attr(x[[1]], "members") <- 1
attr(x[[2]], "members") <- 1

## set class as "dendrogram" 
class(x) <- "dendrogram"
x
plot(x)

【问题讨论】:

    标签: r dendrogram


    【解决方案1】:

    您可以创建一个函数来构建叶子。添加属性的高度和总高度。 n 和 n1 是 A 和 B 的叶子,n2 是组合的叶子,通过更改类转换为树状图。

    Attr = function(o, plus_) {
            if (!missing(plus_)) for (n in names(plus_)) { attr(o, n) = plus_[[n]]; }
            o
    }
    
    n = Attr("A", list(label = "A", members = 1, height = 0.2, leaf = T));
    n1 = Attr("B", list(label = "B", members = 1, height = 0.3, leaf = T));
    
    n2 = Attr(list(n, n1), list(members = 2, height = 1, midpoint = 0.5));
    class(n2) = 'dendrogram';
    plot(n2)
    
    

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 2014-03-19
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 2019-12-26
      • 2020-04-13
      • 1970-01-01
      • 2015-11-05
      相关资源
      最近更新 更多