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.