Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add ColSideColors on heatmap.2 after performing bi-clustering (row and column)

I have the following code:

library(gplots)
library(RColorBrewer);

setwd("~/Desktop")
mydata <- mtcars
hclustfunc <- function(x) hclust(x, method="complete")
distfunc <- function(x) dist(x,method="euclidean")

d <- distfunc(mydata)
fit <- hclustfunc(d)
clusters <- cutree(fit, h=100)
nofclust.height <-  length(unique(as.vector(clusters)));

# Colorings
hmcols <- rev(redgreen(2750))
selcol <- colorRampPalette(brewer.pal(12,"Set3"))
selcol2 <- colorRampPalette(brewer.pal(9,"Set1"))
clustcol.height = selcol2(nofclust.height);

heatmap.2(as.matrix(mydata), 
           trace='none', 
           dendrogram='both', 
           key=F,
           Colv=T, 
           scale='row',
           hclust=hclustfunc, distfun=distfunc, col=hmcols,
           symbreak=T,
           margins=c(7,10), keysize=0.1,
           lwid=c(5,0.5,3), lhei=c(0.05,0.5),
           lmat=rbind(c(5,0,4),c(3,1,2)),
           labRow=rownames(mydata),
           #ColSideColors=clustcol.height[clusters],  # This line doesn't work
           RowSideColors=clustcol.height[clusters])

Which produce the following figure: enter image description here

What I want to do is to perform clustering on both row and column and show clustering bars (RowSideColors and ColSideColors) next to dendogram. How can I achieve that?

At the moment I only succeed in showing RowSideColors but not the ColSideColors one.

like image 770
pdubois Avatar asked Mar 09 '14 04:03

pdubois


1 Answers

In order to show both RowSideColors and ColSideColors you have to obtain cluster assignments for rows and columns of the matrix separately. At the moment object 'cluster' contains clusters that correspond to rows only.

# set the custom distance and clustering functions, per your example
hclustfunc <- function(x) hclust(x, method="complete")
distfunc <- function(x) dist(x, method="euclidean")

# perform clustering on rows and columns
cl.row <- hclustfunc(distfunc(mydata))
cl.col <- hclustfunc(distfunc(t(mydata)))

# extract cluster assignments; i.e. k=8 (rows) k=5 (columns)
gr.row <- cutree(cl.row, 8)
gr.col <- cutree(cl.col, 5)

# require(RColorBrewer)
col1 <- brewer.pal(8, "Set1")
col2 <- brewer.pal(5, "Pastel1")

# require(gplots)    
heatmap.2(as.matrix(mydata), hclustfun=hclustfunc, distfun=distfunc,   
          RowSideColors=col1[gr.row], ColSideColors=col2[gr.col])

You can examine the clustering a priori with plot(cl.row) and plot(cl.col). Also you may play with the RColorBrewer library to select the most appropriate colour-coding. Possibly a sequential palette may be better to avoid over-colouring.

like image 166
TWL Avatar answered Nov 11 '22 08:11

TWL