-5

This thread explains how to create a matrix from x/y/z coordinates using the akima package but I'd rather not use a new package. After all, you can do the same thing in one command in gnuplot: gnuplot interpolation.

The image plot using a matrix of data points in the above gnuplot thread can be achieved in R with a call to the image() command.

What about a surface plot, how can we interpolate the x/y/z points to generate a 2d heatmap?

Community
  • 1
  • 1
Robert Kubrick
  • 8,413
  • 13
  • 59
  • 91
  • 2
    What do you mean by *the standard R commands?* Everything in `base`? or what is loaded when you open R? Are you happy enough to use the core packages that are (usually) included with R? – mnel Feb 06 '13 at 22:30
  • The approach in the linked thread is what is described for 2d interpolation in the MASS book. Sounds pretty standard to me... – cbeleites unhappy with SX Feb 06 '13 at 23:21
  • @mnel Alright, alright... I just wanted to make sure there wasn't an easier way to plot an interpolated matrix using the core packages. No reason to get tense. – Robert Kubrick Feb 06 '13 at 23:44

1 Answers1

7

Well, if you don't like akima::interp, maybe stats::loess is to your taste?

topo.loess <- loess (z ~ x * y, topo, degree = 2, span = 0.2)
x <- seq (min (topo$x), max (topo$x), .05)
y <- seq (min (topo$y), max (topo$y), .05)
interpolated <- predict (topo.loess, expand.grid (x = x, y = y))
image (x= x, y= y, z = interpolated, asp = 1)
points (topo)

(also along the lines of MASS)

smoothed interpolation

Otherwise: why not use gnuplot? Though it may be considered a different piece of software as well...

cbeleites unhappy with SX
  • 13,717
  • 5
  • 45
  • 57
  • I just asked this because I've found weird we can plot a matrix image in one command, but we have to use a special package to run a simple interpolation. Very enough, thanks. – Robert Kubrick Feb 06 '13 at 23:47
  • 1
    @RobertKubrick: you may notice that this interpolation is not that simple: it is not restricted to data that is already on an even grid, and `loess` is quite a sophisticated smoothing interpolation. Besides, I think it reflects R's emphasis on statistics together with the fact that there is not just "simple" interpolation but different interpolating models. I really recommend Ch. 15 of the MASS book. – cbeleites unhappy with SX Feb 06 '13 at 23:52