This page is dedicated to the heatmap() native function of R. A heatmap is a table of numbers, where you substitute the numbers with colored cells. There are other ways to draw heatmap and a section of the gallery is dedicated to it! Concerning the input data format, you need to have a numeric matrix. Let’s have a look to the mtcar dataset provided by R that we will use in the following tutorial.


Basic Heatmap & Normalization


It is quite straightforward to draw the default heatmap (on the left): just call the function without any argument. Here, red cells denotes small values, and red small ones. This heatmap is not really informative. Indeed, the hp and disp variable have really high value, what makes that the other variables with small values all look the same. Thus, we need to normalize this matrix. This is done using the scale argument (on the right). It can be applied to rows or to columns following your needs


Control Dendrograms & reordering

You may be noticed that order of both rows and columns is different compare to the native mtcar matrix. This is because heatmap do a reordering using clusterisation: it calculates the distance between each pair of rows and columns and try to order them by similarity. Moreover, the corresponding dendrogram are provided beside the heatmap. We can avoid it and just visualize the raw matrix: use the Rowv and Colv arguments as follow.


Custom Colors


There are several ways to custom the color palette:

  •  use the native palettes of R: terrain.color, rainbow, heat.colors, topo.colors or cm.colors
  •  use the palettes proposed by RColorBrewer. (See graph #38 and graph #40)


Custom Layout




You can custom title & axis titles with the usual main and xlab/ylab arguments (left). You can also change labels with labRow/colRow and their size with cexRow/cexCol.


Add colors beside Heatmap


Often, we do heatmap to compare the observed structure with the expected one. You can add a vector of colors beside the heatmap that represents the expected strucute. Here, I choosed to add a color following the first letter of each car names.

Not what you are looking for ? Make a new search !


Leave a Reply

1 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
1 Comment authors
Ashish Nelson Recent comment authors
newest oldest most voted
Notify of
Ashish Nelson
Ashish Nelson

Is there a way to move the names of the cars to the right side of the heatmap and the (mpg,cyl etc ) bit to the top of the heatmap?