Tal Galili Tal Galili - 1 month ago 8
R Question

is there a way to get a "subtree" from hclust ? (R)

I wish to create a "subtree" from an hclust object.

For example, let's say I have the following object:

a <- list() # initialize empty object
a$merge <- matrix(c(-1, -2,
-3, -4,
1, 2,
-5,-6,
3,4), nc=2, byrow=TRUE )
a$height <- c(1, 1.5, 3,4,4.5) # define merge heights
a$order <- 1:6 # order of leaves(trivial if hand-entered)
a$labels <- 1:6# LETTERS[1:4] # labels of leaves
class(a) <- "hclust" # make it an hclust object
plot(a) # look at the result


Now I wish the extract from it the following subtree:

a <- list() # initialize empty object
a$merge <- matrix(c(-1, -2,
-3, -4,
1, 2
), nc=2, byrow=TRUE )
a$height <- c(1, 1.5, 3) # define merge heights
a$order <- 1:4 # order of leaves(trivial if hand-entered)
a$labels <- 1:4# LETTERS[1:4] # labels of leaves
class(a) <- "hclust" # make it an hclust object
plot(a) # look at the result


How could I access it?

(I know that cutree could get me the objects of the sub tree, but not create an actual hclust object)

Thanks for any help,

Tal

Answer

Not sure this is what you're looking for, but you could

a <- as.dendrogram(a)
branch1 <- a[[1]]
branch2 <- a[[2]]

par(mfrow=c(1,3))
plot(a)
plot(branch1)
plot(branch2)