Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
手动创建树状图r_R_Dendrogram - Fatal编程技术网

手动创建树状图r

手动创建树状图r,r,dendrogram,R,Dendrogram,我正在尝试根据我不是通过hclust或任何其他方法获得的相似性分数创建一个树状图。我有两个分支,只想根据它们的相似程度将它们画出来,然后将它们分支 A和B为0.5相似 A是0.2唯一的 B是0.3唯一的 所以A的总高度是0.7,B的总高度是0.8,其中0.5个分支是共享的 下面只做了两个分支,没有连接两片叶子的长分支。是的,但是没有多大帮助 x <- list(1, 2) ## attach "leaf" and "label" attributes to leaf nodes attr(

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

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

所以A的总高度是0.7,B的总高度是0.8,其中0.5个分支是共享的

下面只做了两个分支,没有连接两片叶子的长分支。是的,但是没有多大帮助

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)

x您可以创建一个函数来构建叶子。添加属性的高度和总高度。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)


您可以创建一个函数来构建叶子。添加属性的高度和总高度。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)