# [BioC] using cutree() to figure out the labels of the left cluster on a heatmap

Thomas Girke thomas.girke at ucr.edu
Mon May 28 23:29:14 CEST 2007

```Andrew,

I am not sure, but is this what you are looking for:

# Sample dendrogram
y <- matrix(rnorm(50), 10, 5, dimnames=list(paste("t", 1:10, sep=""), paste("g", 1:5, sep="")))
hc <- hclust(dist(y))
hcd1 <- as.dendrogram(hc)

# Reorder the dendrogram
hcd2 <- reorder(hcd1, sample(1:10))

# Plot the two dendrograms
par(mfrow=c(2,1))
plot(hcd1, main="Original tree")
plot(hcd2, main="Reordered tree")

# Tree cutting
mycut <- cutree(hc, k=2)

# Cutree() clusters in original tree
cl1 <- mycut[labels(hcd1)]
cl1

# Cutree() clusters in reordered tree
cl2 <- mycut[labels(hcd2)]
cl2

# Cluster orientation in tree or heatmap for cl1
if(unique(cl1)[1]==1){
cat("Cluster 1 is on left and cluster 2 is on right \n" )
} else {
cat("cluster 1 is on right and cluster 2 on left \n" )
}

# Cluster orientation in tree or heatmap for cl2
if(unique(cl2)[1]==1){
cat("Cluster 1 is on left and cluster 2 is on right \n" )
} else {
cat("cluster 1 is on right and cluster 2 on left \n" )
}

Thomas

On Mon 05/28/07 15:05, Andrew Yee wrote:
>
> I'm still trying to figure out if there's a simple solution to figuring out
> if the cluster assigned to say "1" or "2" is associated with the left or
> right of the heatmap, after the dendrogram has been reordered.
>
> A clunky solution to this is:
>
> hcc <-hclust(cor.dist(t(active.matrix))) #where cor.dist is a distance
> function
> ddc <- as.dendrogram(hcc)
>
> ColV <- colMeans(active.matrix)
>
> ddc <- reorder(ddc,ColV)
> reordered.labels <- labels(ddc)
>
>  category<-data.frame(as.numeric(cutree(hcc,k=2)))
>
> And then use the reordered.labels vector to figure out the cluster that's
> "1" or "2" in the category data.frame
>
> Thanks,
> Andrew
>
> On 5/28/07, Thomas Girke <thomas.girke at ucr.edu> wrote:
> >
> > Andrew,
> >
> > If I understand you correctly, then the hclust components 'labels' and
> > 'order' contain
> > the information you are looking for. Below is a short example that
> > illustrates how to
> > access this data. More details on this topic including dendrogram coloring
> > utilities
> >
> > http://faculty.ucr.edu/~tgirke/Documents/R_BioCond/R_BioCondManual.html#R_clustering
> >
> > # Generate a sample hclust object and plot it with heatmap
> > y <- matrix(rnorm(50), 10, 5, dimnames=list(paste("g", 1:10, sep=""),
> > paste("t", 1:5, sep="")))
> > hr <- hclust(as.dist(1-cor(t(y), method="pearson")), method="complete")
> > hc <- hclust(as.dist(1-cor(y, method="spearman")), method="complete")
> > heatmap(y, Rowv=as.dendrogram(hr), Colv=as.dendrogram(hc), scale="row")
> >
> > # Print the dendrogram labels (hr) in the order they appear in the tree.
> > hr\$labels[hr\$order]
> >
> > # Cut the row tree by height level
> > mycl <- cutree(hr, h=max(hr\$height)/1.5)
> >
> > # Print the obtained cluster numbers in tree order
> > mycl[hr\$order]
> >
> > # Plot the heatmap with color bar where the row colors correspond to the
> > obtained clusters
> > mycol <- sample(rainbow(256)); mycol <- mycol[as.vector(mycl)]
> > heatmap(y, Rowv=as.dendrogram(hr), Colv=as.dendrogram(hc), scale="row",
> > RowSideColors=mycol)
> >
> >
> >
> > Thomas
> >
> >
> > On Sun 05/27/07 22:22, Andrew Yee wrote:
> > > I've been using heatmap(), hclust(), and cutree(), and I'm trying to
> > figure
> > > out the column labels of the left (as opposed to the right) cluster.
> > >
> > > Using cutree(x, k=2), I get two clusters labeled 1 and 2.  How do you
> > figure
> > > out if cluster 2 is the one associated with the left cluster on the
> > heatmap
> > > (instead of manually going to the heatmap)?
> > >
> > > Thanks,
> > > Andrew
> > >
> > > Andrew Yee, MD
> > > MGH Cancer Center
> > >
> > >       [[alternative HTML version deleted]]
> > >
> > > _______________________________________________
> > > Bioconductor mailing list
> > > Bioconductor at stat.math.ethz.ch
> > > https://stat.ethz.ch/mailman/listinfo/bioconductor
> > > Search the archives:
> > http://news.gmane.org/gmane.science.biology.informatics.conductor
> > >
> >
> > --
> > Dr. Thomas Girke
> > Assistant Professor of Bioinformatics
> > Director, Bioinformatics Facility
> > Center for Plant Cell Biology
> > Department of Botany and Plant Sciences
> > 1008 Noel T. Keen Hall
> > University of California
> > Riverside, CA 92521
> >
> > E-mail: thomas.girke at ucr.edu
> > Website: http://faculty.ucr.edu/~tgirke
> > Ph: 951-827-2469
> > Fax: 951-827-4437
> >
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
>

--
Dr. Thomas Girke
Assistant Professor of Bioinformatics
Director, Bioinformatics Facility
Center for Plant Cell Biology
Department of Botany and Plant Sciences
1008 Noel T. Keen Hall
University of California
Riverside, CA 92521

E-mail: thomas.girke at ucr.edu
Website: http://faculty.ucr.edu/~tgirke
Ph: 951-827-2469
Fax: 951-827-4437

```