R heatmap.2 (gplots) access dendogram data

1k views Asked by At

In R I use heatmap.2 (from the gplots package) to cluster and visualize some data.

I would like to access the column dendrogram (clusters) for further manipulation of my data.

For example:

x = matrix(runif(250), nrow= 50) h = heatmap.2(x)

h$colDendrogram 
'dendrogram' with 2 branches and 5 members total, at height 3.033438

Is there a way to know the indexes of the column belonging to the first branch and those belonging to the second (in an automatic fashion ; of course in this simple case I might just look at labels on the x axis).

Further how can I access the other sub branches?

1

There are 1 answers

0
lucacerone On BEST ANSWER

One can use the as.hclust() function and treat the resulting object as if it were an R hclust object.

For the specific case presented in the question this is how one access the column dendrogram:

colhclust = as.hclust(h$colDendrogram)
groups = cutree(cl,2)

groups is a vector containing the group of each column;

id_g1 = which(groups == 1)

contains the indexes of items belonging to the first branch.