interp(x, y, z, xo=<<see below>>, yo=<<see below>>, ncp=0, extrap=F)
x, y, and z must be the same length and may contain no fewer than four points. The points of x and y cannot be collinear, i.e, they cannot fall on the same line (two vectors x and y such that y = ax + b for some a, b will not be accepted). interp is meant for cases in which you have x, y values scattered over a plane and a z value for each. If, instead, you are trying to evaluate a mathematical function, or get a graphical interpretation of relationships that can be described by a polynomial, try outer().
The interp function can not handle duplicate (x,y) points (since it wouldnt know what z value to assign at such points). If you have such data, you need to reduce each duplicate point to a single point - giving it, perhaps, the mean or median of the corresponding z values. Or, if your z data is relatively smooth, you can use jitter() on your x or y data.
The triangulation scheme used by interp works well if x and y have similar scales but will appear stretched if they have very different scales. The spreads of x and y must be within four orders of magnitude of each other for interp to work.
# an example with duplicate (x,y) points in the data airtemp.jit <- jitter(air$temperature) air.jit <- interp(air$radiation, airtemp.jit, air$ozone) # gives error#an example averaging the z data over duplicate (x,y) points f <- function(x = air$temperature, y = air$radiation, z = air$ozone, FUN = median) { xy <- paste(x, y, sep = ",") i <- match(xy, xy) z.smoothed <- unlist(lapply(split(z, i), FUN)) ord <- !duplicated(xy) data.frame(x = x[ord], y = y[ord], z = z.smoothed) } air.smooth<- f() air.grid <- interp(air.smooth$x, air.smooth$y, air.smooth$z)
oz <- interp(ozone.xy$x, ozone.xy$y, ozone.median) contour(oz) #contour plot
#example changing xo and yo to get very fine grid akima.smooth<- interp(akima.x, akima.y, akima.z, xo= seq(0,25, length=100), yo= seq(0,20, length=100)) persp(akima.smooth)