display a matrix, including the values, as a heatmap

For example:

m <- matrix(1:30, ncol=6)
colnames(m) <- paste("C", 1:6, sep="")
rownames(m) <- paste("R", 1:5, sep="")
m

image(1:ncol(m), 1:nrow(m), t(m), col = terrain.colors(60), axes = FALSE)
axis(1, 1:ncol(m), colnames(m))
axis(2, 1:nrow(m), rownames(m))
for (x in 1:ncol(m))
  for (y in 1:nrow(m))
    text(x, y, m[y,x])

Try heatmap.2 from the gplots package. The cellnote and notecol parameters control the text placed in cells. You'll probably want dendrogram = "none" as well.


You can use image and text. I personally like image.plot from the fields package, because it adds a legend on the side, but you can use it with image too.

So for instance

require(fields)
# Make a 10x10 matrix
m = matrix(rnorm(100), nrow=10)
image.plot(m)
for (x in 1:10)
    for (y in 1:10)
        text((x-1)/9, (y-1)/9, sprintf("%0.2f", m[x,y]))

Tags:

R

Heatmap