1

I have an input file of this form:

 0.35217720               1         201           1
 0.26413283               1         209           1
 1.1665874                1         210           1
 ...
 0.30815500               2         194           1
 0.15407741               2         196           1
 0.15407741               2         197           1
 0.33016610               2         205           1
 ...

where the first column is a scalar value, the second is the x coordinate of a discrete lattice, the third is the y coordinate and the last one is time-like discrete component. I would like to make a two dimensional heatmap of the scalar values at fixed time. How can i do? Edit: I don't know how to use image() to use the second and the third column as x, y coordinates.

Example file:

  7.62939453              1           1           1
  1.3153768               1           2           1
  7.5560522               1           3           1
  4.5865011               1           4           1
  5.3276706               1           5           1
  2.1895909               2           1           1
  0.47044516              2           2           1
  6.7886448               2           3           1
  6.7929626               2           4           1
  9.3469286               2           5           1
  3.8350201               3           1           1
  5.1941633               3           2           1
  8.3096523               3           3           1
  0.34571886              3           4           1
  0.53461552              3           5           1
  5.2970004               4           1           1
  6.7114925               4           2           1
  7.69805908              4           3           1
  3.8341546               4           4           1
  0.66842079              4           5           1
  4.1748595               5           1           1
  6.8677258               5           2           1
  5.8897662               5           3           1
  9.3043633               5           4           1
  8.4616680               5           5           1
emanuele
  • 2,519
  • 8
  • 38
  • 56
  • 3
    What have you tried? Have you read `?heatmap` or `?image`? Try looking here: http://stackoverflow.com/questions/6998259/heatmap-generation?rq=1 – Seth Jul 31 '12 at 17:45
  • Can you give a reproducible dataset? – Seth Jul 31 '12 at 17:45
  • Review this, try it. Come back and edit your question with what you need help with. http://learnr.wordpress.com/2010/01/26/ggplot2-quick-heatmap-plotting/ – Brandon Bertelsen Jul 31 '12 at 17:49
  • @BrandonBertelsen i cannot use gglot2, my version of R does not support it. – emanuele Jul 31 '12 at 18:39
  • @Seth i am sorry but the file is huge, aprox 1 GB. – emanuele Jul 31 '12 at 18:40
  • 1
    @emanuele... So maybe we don't need the whole file. Perhaps you could construct small [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), maybe on a 10x10 lattice with only 2 times (or maybe time isn't necessary because if you could get an answer for a single time you could handle the rest). – Gregor Thomas Jul 31 '12 at 18:49

2 Answers2

2

Reshape your data to a matrix and then use heatmap():

This worked on R version 2.10.1 (2009-12-14):

txt <- textConnection("7.62939453              1           1           1
  1.3153768               1           2           1
  7.5560522               1           3           1
  4.5865011               1           4           1
  5.3276706               1           5           1
  2.1895909               2           1           1
  0.47044516              2           2           1
  6.7886448               2           3           1
  6.7929626               2           4           1
  9.3469286               2           5           1
  3.8350201               3           1           1
  5.1941633               3           2           1
  8.3096523               3           3           1
  0.34571886              3           4           1
  0.53461552              3           5           1
  5.2970004               4           1           1
  6.7114925               4           2           1
  7.69805908              4           3           1
  3.8341546               4           4           1
  0.66842079              4           5           1
  4.1748595               5           1           1
  6.8677258               5           2           1
  5.8897662               5           3           1
  9.3043633               5           4           1
  8.4616680               5           5           1
")
df <- read.table(txt)
close(txt)

names(df) <- c("value", "x", "y", "t")

require(reshape)
dfc <- cast(df[ ,-4], x ~ y)
heatmap(as.matrix(dfc))
EDi
  • 13,160
  • 2
  • 48
  • 57
  • the x and y axes range from 0 to 1. How can i tell to R to range from 0 to 360 for the x axes and 0 to 180 for the y axes? – emanuele Aug 05 '12 at 08:29
1
## Some copy/pasteable fake data for you (dput() works nicely for pasteable real data)
your_matrix <- cbind(runif(25, 0, 10), rep(1:5, each = 5), rep(1:5, 5), rep(1, 25))

heatmap_matrix <- matrix(your_matrix[, 1], nrow = 5)
## alternatively, if your_matrix isn't in order
## (The reshape method in EDi's answer is a nicer alternative)
for (i in 1:nrow(your_matrix)) {
    heatmap_matrix[your_matrix[i, 2], you_matrix[i, 3]]
}

heatmap(heatmap_matrix) # one option
image(z = heatmap_matrix) # another option
require(gplots)
heatmap.2(heatmap_matrix) # this has fancier preferences
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294