tags:

views:

113

answers:

5

What I'd like to do is take this matrix:

> partb
                0.5  1.5   1a   1b   -2   -3
A1FCLYRBAB430F 0.26 0.00 0.74 0.00 0.00 0.00
A1SO604B523Q68 0.67 0.33 0.00 0.00 0.00 0.00
A386SQL39RBV7G 0.00 0.33 0.33 0.33 0.00 0.00
A3GTXOXRSE74WD 0.41 0.00 0.08 0.03 0.05 0.44
A3OOD9IMOHPPFQ 0.00 0.00 0.33 0.00 0.33 0.33
A8AZ39QM2A9SO  0.13 0.54 0.18 0.13 0.00 0.03

And then make a heatmap that has each of the values in the now colored cells.

Making a heatmap is easy:

> heatmap( partb, Rowv=NA, Colv=NA, col = heat.colors(256),  margins=c(5,10))

But for the life of me I can't figure out how to put the value in each of the cells.

What am I missing? Surely this is a common thing.

A: 

levelplot() from the lattice package will give you a color legend. Not exactly what you want but something to think about.

Maiasaura
+1  A: 

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.

Connor M
This works well, but the spacing is all messed up. The top left of the image, where the key was, is just blank. Any ideas on how to center it: heatmap.2( partb, Rowv=FALSE, Colv=FALSE, dendrogram='none', cellnote=partb, notecol="black", trace='none', rowsep=c(1,2,3,4,5,6), key=FALSE)
Nathan VanHoudnos
Good point. The heatmap.2 function actually uses the layout function and creates 4 plots it the output. You could try the `lmat`, `lwid`, and `lhei`, params, or modify the source of the function to do what you need, but I've haven't gone that far with it.
Connor M
This was exactly was what I needed. Messing with `lwid` and `lhei` worked perfectly. Setting `margins` allowed me to make sure the labels weren't cut off. Whole thing: `heatmap.2( partb, Rowv=FALSE, Colv=FALSE, dendrogram='none', cellnote=partb, notecol="black", trace='none', key=FALSE,lwid = c(.01,.99),lhei = c(.01,.99),margins = c(5,15 ))`
Nathan VanHoudnos
A: 

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]))
nico
A: 

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])
lcgong
A: 

Here's an example using ggplot2

http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/

Brandon Bertelsen