Heat Maps With ggplot2

Heat maps graphical represent data values as matrix of colored tiles. This data visualization can be extremely useful to visualize the distribution values. In R, you can create heat maps using the heatmap function. However, using ggplot2, you can create heat maps that are not only useful, but also look great.

To illustrate how to use heat maps with ggplot2, I use the default example provided by the manual page for heatmap.

> library(RColorBrewer)
> x  <- as.matrix(mtcars)
> rc <- rainbow(nrow(x), start = 0, end = .3)
> cc <- rainbow(ncol(x), start = 0, end = .3)
> heatmap(x, Rowv=NA, Colv=NA, col=brewer.pal(9, "Blues")[1:9], scale="none", margins=c(5,10), revC=T)

In this example, we already used a custom color theme as the default color theme is really disturbing. The result looks like this:


Although this looks correct, there exists an issue: the number of colors that are displayed are limited to 9 colors. Thus, the heat map does not show the full variance of value differences.
Let’s do the same heat map with ggplot2. But first, we have to switch to a different data format. ggplot2 expects data in a different format—a format in which columns are melted down into rows and differentiated by measurement and value columns: for more detail see the reshape package description.

> library(reshape)
> y <- melt(x)

Using this data format (we did not modify the data only the format), we can use ggplot2 to create the very same heat map:

> library(ggpot2)
> p <- ggplot(y, aes(y=X1,x=X2))
> p + geom_tile(aes(fill=value)) + scale_fill_gradient(low="white", high="darkblue") + xlab("") + ylab("")

The final result looks similar but shows much more differentiated tiles and thus much more details.
Changing colors is easy. Simply provide different string or hex values in the scale_fill_gradient function.