1
votes

I am trying to create a dendrogram from similarity scores I have acquired not through hclust or any other means. I have two branches and just want to draw them out according to how similar they are and then have them branch off.

A and B are 0.5 similar A is 0.2 unique B is 0.3 unique

So the total height of A is 0.7 and the total height of B is 0.8, where 0.5 of their branches are shared.

The following just makes two branches without a long branch connecting the two leaves. There is this similar question, but it doesn't quite help!

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)
1

1 Answers

0
votes

You can make a function to build the leaves. Add the height of the attributes and the total height. n and n1 are the leaves for your A and B and n2 are your leaves combined and are converted to a dendrogram by changing the class.

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)