How to make a heatmap in R with xyz dissimilar data

gnuplot, graph, heatmap, r

Solution

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

Problem

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.

Original source

Related problems