1

我有一个这种形式的输入文件:

 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
 ...

其中第一列是标量值,第二列是离散格的 x 坐标,第三列是 y 坐标,最后一列是类时间离散分量。我想在固定时间制作标量值的二维热图。我能怎么做?编辑:我不知道如何使用 image() 将第二列和第三列用作 x、y 坐标。

示例文件:

  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
4

2 回答 2

2

将数据重塑为矩阵,然后使用 heatmap():

这适用于 R 版本 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))
于 2012-07-31T20:03:24.853 回答
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
于 2012-07-31T20:09:51.520 回答