How do I manually create a dendrogram (or "hclust") object ? (in R)

Tal Galili picture Tal Galili · Feb 22, 2010 · Viewed 13k times · Source

I have a dendrogram given to me as images. Since it is not very large, I can construct it "by hand" into an R object.

So my question is how do I manually create a dendrogram (or "hclust") object when all I have is the dendrogram image?

I see that there is a function called "as.dendrogram" But I wasn't able to find an example on how to use it.

(p.s: This post is following my question from here)

Many thanks, Tal

Answer

Aniko picture Aniko · Feb 22, 2010

I think you are better of creating an hclust object, and then converting it to a dendrogram using as.dendrogram, then trying to create a dendrogram directly. Look at the ?hclust help page to see the meaning of the elements of an hclust object.

Here is a simple example with four leaves A, B, C, and D, combining first A-B, then C-D, and finally AB-CD:

a <- list()  # initialize empty object
# define merging pattern: 
#    negative numbers are leaves, 
#    positive are merged clusters (defined by row number in $merge)
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 <- LETTERS[1:4]    # labels of leaves
class(a) <- "hclust"        # make it an hclust object
plot(a)                     # look at the result   

#convert to a dendrogram object if needed
ad <- as.dendrogram(a)