Monday, March 2, 2015

Heatmap (I): heatmap()



Abstract: Graphics of heatmap using the basic function stats:heatmap() in R.


A heat map is a false color image (the R function image() ) with dendrograms added to the left side (clustering by rows) or to the top (clustering by columns). The reordering of the rows and columns are consistent with values of rows or columns within the restrictions imposed by the dendrogram is carried out. The basic function of drawing a heatmap in R is heatmap().
Usage:
heatmap(x, Rowv = NULL, Colv = if(symm)"Rowv" else NULL, distfun = dist, hclustfun = hclust,
scale = c("row", "column", "none"), na.rm = TRUE,
margins = c(5, 5), ColSideColors, RowSideColors,
cexRow = 0.2 + 1/log10(nr), cexCol = 0.2 + 1/log10(nc),
labRow = NULL, labCol = NULL, main = NULL, xlab = NULL, ylab = NULL )
Note: x should be matrix. Data frame would result in errors.
x<-as.matrix(scale(mtcars))
heatmap(x, scale="none")



Dengrogramm
‘Rowv’ and ‘Colv’ control how the rows and columns of the data set should be reordered.
If the mode Rowv/Colv is NULL as defaults. Clustering is based on the hclustfun and distfun parameters. This default linkage clustering implements the measuring of euclidean distance. The dendrogram is reordered using the row and column means.
heatmap(x, Rowv=NULL, scale="none", main='Rowv=NULL')# the default

If the mode Rowv/Colv is NA. No reordering or clustering is done and the colorful matrix is drawn as the values are.
heatmap(x, Rowv=NA, scale="none", main='Rowv=NA')#supress the dendrogram fo rows


If the mode of Rowv/Colv is a dendrogram object, this dendrogram is honoured and used to reorder the matrix. The dendrogram objects can be generated separately according to a personal way, instead of the specifications by the default dendrogram functions hclust() and dist().
rowDistance = dist(x, method = "manhattan")
rowCluster = hclust(rowDistance, method = "complete")
rowDend = as.dendrogram(rowCluster)
rowDend = reorder(rowDend, rowMeans(x))
heatmap(x, Rowv=rowDend, scale="none", main='Rowv is a dendrogram object')


Color grids
The default colors in heatmap() is not so sourceful. There are many other color palattes.
# personal color palette
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 100)
heatmap(x, col=my_palette, scale="none", main='Color palette (from red to yellow)')
my_palette <- colorRampPalette(c("red", "black", "green"))(n = 100)
heatmap(x, col=my_palette, scale="none", main='Color palette (from red to green)')




heatmap(x, col=topo.colors(100), scale="none", main='topo.colors')



#
library(gplots)
heatmap(x, col=redgreen(100), scale="none", main='topo.colors')



Writing date: 2015.03.02

No comments:

Post a Comment