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.
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'.
Here, the minimum value of dissimilarity is 0 (identical) and maximum is somewhere around 3. You can make a custom color scheme using colorRampPalette():
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.
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.
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.
R Resources >