R Resources‎ > ‎

Heatmap displays

A heat map figure can come in handy when displaying a correlation matrix or distance matrix (or any kind of matrix, for that matter). In essence, you are using colors to represent the relative values of the cells in a matrix. 

We'll first make a sample distance matrix that represents the dissimilarity between 20 fictional species based on a single variable, e.g., size. We'll sort the data first so that species 1 will be close in size to species 2, and so on. 

size=rnorm(20)
d=data.frame(sort(size))
dis=dist(d,diag=TRUE)
m=as.matrix(dis)
This will give us a symmetrical matrix of 'distances' between each species. The diagonal should all be 0, since all species will be perfectly similar to itself. 

Now we want to display this distance matrix as a heat map. By far the easiest way to do this is to use the levelplot() function in the package 'lattice'.

library(lattice)
levelplot(m[1:ncol(m),ncol(m):1])


Here, the minimum value of dissimilarity is 0 (identical) and maximum is somewhere around 3. You can make a custom color scheme using colorRampPalette():

new.palette=colorRampPalette(c("black","red","yellow","white"),space="rgb")
levelplot(m[1:ncol(m),ncol(m):1],col.regions=new.palette(20))

You can also recreate this figure without relying on the package 'lattice'. The function image() is useful for this. The rest of the following code has to do with the layout.


quartz(width=7,height=6) #make a new quartz window of a given size
par(mar=c(2,3,2,1)) #set the margins of the figures to be smaller than default
layout(matrix(c(1,2),1,2,byrow=TRUE),widths=c(7,1)) #set the layout of the quartz window. This will create two plotting regions, with width ratio of 7 to 1
image(m[1:ncol(m),ncol(m):1],col=new.palette(20),xaxt="n",yaxt="n") #plot a heat map matrix with no tick marks or axis labels
axis(1,at=seq(0,1,length=20),labels=rep("",20)) #draw in tick marks
axis(2,at=seq(0,1,length=20),labels=rep("",20))

#adding a color legend
s=seq(min(m),max(m),length=20) #20 values between minimum and maximum values of m
l=matrix(s,ncol=length(s),byrow=TRUE) #coerce it into a horizontal matrix
image(y=s,z=l,col=new.palette(20),ylim=c(min(m),max(m)),xaxt="n",las=1) #plot a one-column heat map


A related function is heatmap(), which implements a hierarchical clustering algorithm and then displays a dendogram and heatmap matrix. You can control the color scheme the same way as before. Here, I use the symm=TRUE argument because it is a symmetrical matrix.
heatmap(m,symm=TRUE,col=new.palette(20))
You'll notice though that the row and column orders have been re-ordered based on the hierarchical clustering results. But remember that branches of the tree can be flipped around. In the above example, it looks as though species 1 and 2 are closely related to species 20, but this is not the case. if you flip the tree around at the second branching node, you can get species 20 to be far from species 1 and 2. In fact, it should be possible to flip the branches around until you get the rows/columns to be ordered as 1,2,3,4... while keeping the same branches (this is because we ordered the species according to size to begin with in the example dataset above). So, the heatmap() function is useful if you are using hierarchical clustering methods, but may not be appropriate for other applications.


Comments