Table of Numbers to Image

Sometimes you want to bring a little life or nuance to a table of numbers to emphasize certain values. You can do this pretty easily with R, although it's not very straightforward.

Starting with a table of numbers:

               mst1   gcn5  mst2 elp3    gm    ge    me   gme
  mei2         3.00  3.000  1.00 -2.0  4.00  0.20 -1.00  3.00
  SPAC1039.02 -3.00 -2.000 -2.00  1.0 -5.00  0.70  0.90 -3.00
  zym1         5.00 -0.800  0.50 -1.0  0.80 -2.00 -1.00  3.00
  SPAC186.05c  0.40 -4.000 -4.00 -4.0 -5.00 -0.90  0.80 -3.00
  SPAC186.06  -0.50 -3.000 -4.00 -3.0 -3.00 -0.20  2.00 -3.00
  act1         0.10  0.050  0.04 -0.1  0.09 -0.10 -0.07 -0.03
  pgk1        -0.05 -0.002 -0.05 -0.2  0.02 -0.05 -0.20  0.06

Use the R image() function to create a colored grid representing the values:

# reverse table so that it displays properly in image
goi.d <- goi.d[sort(1:nrow(goi.d), decreasing=T),]

#------------------------------------------#
#----        Draw the image map        ----#
#------------------------------------------#
# yellow->white->blue   faded
R <- c(rep(1, times=128), seq(1,0.5,length=128))
G <- c(rep(1, times=128), seq(1,0.5,length=128))
B <- c(seq(0.5,1,length=128), rep(1, times=128))

# create a color ramp
ColorRamp <- rgb(R, G, B)

# c(bottom, left, top, right)
# The default is 'c(4, 5, 4, 2) + 0.1'.
par(mar= c(4, 7, 4, 2))

# use image() to draw the color grid
# use t() to transpose the dataframe
# so it display like you see it
image(t(goi.d), col=ColorRamp, axes=F)

# create a set of points to draw labels
# across the image
x <- seq(from=0, to=1, length=ncol(goi.d))
y <- seq(from=0, to=1, length=nrow(goi.d))

# create a vector for placing text values
series <- 1:length(y)

for( i in series ){
   text(x,y[i],labels=as.character(goi.d[i,]))
}

axis(3, labels=colnames(goi.d), at=x, tick=F)
axis(2, at=y, labels=rownames(goi.d), tick=F, las=2)

R Table2Image (last edited 2009-04-01 23:01:04 by ChrisSeidel)