且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用xyz不相似的数据在R中生成热图

更新时间:2023-11-09 19:39:40

建议的解决方案



使用 scale()来转换x和y使用字段

$ b

模拟



>包在你的问题:

 库(akima)
库(字段)
$ b $ (20,4,3)
y x y< - scale(y)#来重现你的错误
z -norm(20)

s 图像。情节

使用 ggplot2 ,改编自

  library(akima)
library(ggplot2)$ (20,4,3)
y x y< - scale(y)#来重现你的错误
z
t。 < - interp(x,y,z)
t.df< - data.frame(t。)

gt< - data.frame(expand.grid(X1 = t $ x,
X2 = t。$ y),
z = c(t。$ z),
value = cut(c(t。$ z),
(a)(x1,x2,fill = value)bs = bs(min(z),max(z),0.25)))

p geom_tile )+
geom_contour(aes(x = X1,y = X2,z = z),color =black)
p



校正轴标签



另一个问题,解决方案也描述为在重新缩放前使用正确的原始数据值标记轴。目前只适用于 ggplot


I am trying to use the A true heat map in R suggestions, however I get the error:

Error in interp.old(x, y, z, xo = xo, yo = yo, ncp = 0, extrap = extrap,
: scales of x and y are too dissimilar

after the code line:

 s <- interp(x,y,z)

My data was constructed expecting to get coloured heat-map like lines in a dark continuous background, and works in GNUplot using set pm3d map and set hidden3d. The data corresponds to a model of molecules production (y) in a given time (x) with the frequency of appearance denoted by z. It looks like this:

1.000000000000e+00  1e-8    0

1.000000000000e+00  5e-8    0

1.000000000000e+00  1e-7    5

1.000000000000e+00  5e-7    0

1.000000000000e+00  1e-6    0

1.000000000000e+00  5e-6    0

1.000000000000e+00  1e-5    0

1.000000000000e+00  5e-5    0

1.000000000000e+00  1e-4    0

1.000000000000e+00  5e-4    0

1.000000000000e+00  1e-3    0

1.000000000000e+00  5e-3    0

1.000000000000e+00  1e-2    0

1.000000000000e+00  5e-2    0

1.000000000000e+00  1e-1    0

1.000000000000e+00  5e-1    0

1.000000000000e+00  1e+1    0

1.000000000000e+00  5e+1    0

1.000000000000e+00  1e+2    0

1.000000000000e+00  5e+2    0

1.000000000000e+00  1e+3    0

1.000000000000e+00  5e+3    0

1.000000000000e+00  1e+4    0

1.000000000000e+00  5e+4    0

1.000000000000e+00  1e+5    0

1.000000000000e+00  5e+5    0

1.000000000000e+00  1e+6    0

1.000000000000e+00  5e+6    0

1.000000000000e+00  1e+7    0

1.000000000000e+00  5e+7    0

1.000000000000e+00  1e+8    0

1.000000000000e+00  5e+8    0

2.000000000000e+00  1e-8    0

2.000000000000e+00  5e-8    0

2.000000000000e+00  1e-7    0

2.000000000000e+00  5e-7    5

2.000000000000e+00  1e-6    0

2.000000000000e+00  5e-6    0

2.000000000000e+00  1e-5    0

2.000000000000e+00  5e-5    0

2.000000000000e+00  1e-4    0

2.000000000000e+00  5e-4    0

2.000000000000e+00  1e-3    0

2.000000000000e+00  5e-3    0

2.000000000000e+00  1e-2    0

2.000000000000e+00  5e-2    0

2.000000000000e+00  1e-1    0

2.000000000000e+00  5e-1    0

2.000000000000e+00  1e+1    0

2.000000000000e+00  5e+1    0

2.000000000000e+00  1e+2    0

2.000000000000e+00  5e+2    0

2.000000000000e+00  1e+3    0

2.000000000000e+00  5e+3    0

2.000000000000e+00  1e+4    0

2.000000000000e+00  5e+4    0

2.000000000000e+00  1e+5    0

2.000000000000e+00  5e+5    0

2.000000000000e+00  1e+6    0

2.000000000000e+00  5e+6    0

2.000000000000e+00  1e+7    0

2.000000000000e+00  5e+7    0

2.000000000000e+00  1e+8    0

2.000000000000e+00  5e+8    0

3.000000000000e+00  1e-8    0

3.000000000000e+00  5e-8    0

3.000000000000e+00  1e-7    0

3.000000000000e+00  5e-7    0

3.000000000000e+00  1e-6    5

3.000000000000e+00  5e-6    0

3.000000000000e+00  1e-5    0

3.000000000000e+00  5e-5    0

3.000000000000e+00  1e-4    0

3.000000000000e+00  5e-4    0

3.000000000000e+00  1e-3    0

3.000000000000e+00  5e-3    0

3.000000000000e+00  1e-2    0

3.000000000000e+00  5e-2    0

3.000000000000e+00  1e-1    0

3.000000000000e+00  5e-1    0

3.000000000000e+00  1e+1    0

3.000000000000e+00  5e+1    0

3.000000000000e+00  1e+2    0

3.000000000000e+00  5e+2    0

3.000000000000e+00  1e+3    0

3.000000000000e+00  5e+3    0

3.000000000000e+00  1e+4    0

3.000000000000e+00  5e+4    0

3.000000000000e+00  1e+5    0

3.000000000000e+00  5e+5    0

3.000000000000e+00  1e+6    0

3.000000000000e+00  5e+6    0

3.000000000000e+00  1e+7    0

3.000000000000e+00  5e+7    0

3.000000000000e+00  1e+8    0

3.000000000000e+00  5e+8    0

4.000000000000e+00  1e-8    0

4.000000000000e+00  5e-8    0

4.000000000000e+00  1e-7    0

4.000000000000e+00  5e-7    0

4.000000000000e+00  1e-6    0

4.000000000000e+00  5e-6    5

4.000000000000e+00  1e-5    0

4.000000000000e+00  5e-5    0

4.000000000000e+00  1e-4    0

4.000000000000e+00  5e-4    0

4.000000000000e+00  1e-3    0

4.000000000000e+00  5e-3    0

4.000000000000e+00  1e-2    0

4.000000000000e+00  5e-2    0

4.000000000000e+00  1e-1    0

4.000000000000e+00  5e-1    0

4.000000000000e+00  1e+1    0

4.000000000000e+00  5e+1    0

4.000000000000e+00  1e+2    0

4.000000000000e+00  5e+2    0

4.000000000000e+00  1e+3    0

4.000000000000e+00  5e+3    0

4.000000000000e+00  1e+4    0

4.000000000000e+00  5e+4    0

4.000000000000e+00  1e+5    0

4.000000000000e+00  5e+5    0

4.000000000000e+00  1e+6    0

4.000000000000e+00  5e+6    0

4.000000000000e+00  1e+7    0

4.000000000000e+00  5e+7    0

4.000000000000e+00  1e+8    0

4.000000000000e+00  5e+8    0

The first suggestion gave me some ugly results similar the ones obtained at first in A true heat map in R which is a plot with some horizontal lines full of dots in different gray-scale tones. The second seems to crash. I got this message from it:

>Traceback:
1: .Fortran("idsfft", as.integer(1), as.integer(ncp), as.integer(n),     as.double(x),  as.double(y), as.double(z), as.integer(nx),     as.integer(ny), x = as.double(xo), y = as.double(yo), z = zo,     integer((31 + ncp) * n + nx * ny), double(5 * n), misso = as.logical(misso),     PACKAGE = "akima")
2: interp.old(x, y, z, xo = xo, yo = yo, ncp = 0, extrap = extrap,     duplicate = duplicate, dupfun = dupfun)
3: interp(x, y, z)

Possible actions:
1: abort (with core dump, if enabled)
2: normal R exit
3: exit R without saving workspace
4: exit R saving workspace

I just included more data in case it is helpful. I am calling the columns from the data frame this way:

coso <- read.table("/home/libertad/mygraphs/two/two_1_90/coso.txt", header = FALSE,sep = "\t")

>x <-coso[[1]]

>y <-coso[[2]]

This is one of my graphs in GNUplot, I expected to get nicer ones with R.

Suggested Solution

Use scale() to transform x and y to comparable scales.

Simulation

Using the fields package as in your question:

library(akima) 
library(fields) 

x <- rnorm(20, 4, 3)
y <- rnorm(20, 5e-5, 1e-5)
x <- scale(x) # comment out these two lines 
y <- scale(y) # to reproduce your error
z <- rnorm(20)

s <- interp(x,y,z)
image.plot(s)

Using ggplot2, adapted from my other answer here:

library(akima) 
library(ggplot2) 

x <- rnorm(20, 4, 3)
y <- rnorm(20, 5e-5, 1e-5)
x <- scale(x) # comment out these two lines 
y <- scale(y) # to reproduce your error
z <- rnorm(20)

t. <- interp(x,y,z)
t.df <- data.frame(t.)

gt <- data.frame( expand.grid(X1=t.$x, 
                              X2=t.$y), 
                  z=c(t.$z), 
                  value=cut(c(t.$z), 
                            breaks=seq(min(z),max(z),0.25)))

p <- ggplot(gt) + 
    geom_tile(aes(X1,X2,fill=value)) + 
    geom_contour(aes(x=X1,y=X2,z=z), colour="black") 
p

Correcting the Axis Labels

In another question, the solution is also described for labeling the axes with the correct values of the original data before the re-scaling. This currently only applies to ggplot.