Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 792
Tal Galili Avatar asked Feb 22 '10 12:02

Tal Galili


People also ask

How do you make a dendrogram in R?

As you already know, the standard R function plot. hclust() can be used to draw a dendrogram from the results of hierarchical clustering analyses (computed using hclust() function). A simplified format is: plot(x, labels = NULL, hang = 0.1, main = "Cluster dendrogram", sub = NULL, xlab = NULL, ylab = "Height", ...)

How do you make a dendrogram plot?

Specify Number of Nodes in Dendrogram Plot There are 100 data points in the original data set, X . Create a hierarchical binary cluster tree using linkage . Then, plot the dendrogram for the complete tree (100 leaf nodes) by setting the input argument P equal to 0 . Now, plot the dendrogram with only 25 leaf nodes.


1 Answers

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)
like image 103
Aniko Avatar answered Nov 05 '22 04:11

Aniko