Re: [R] interp() function output not continue

From: Duncan Murdoch <>
Date: Tue, 17 Jun 2008 10:03:37 -0400

On 6/17/2008 9:52 AM, Dieter Vanderelst wrote:
> Dear List,
> I'm using interp() to prepare 3d data for plotting with the contour() function.
> If have x,y and z data. All are arrays. X and Y are sampled in an orderly fashion on a grid (a circular sub-area of a grid - see plot). I'm trying to use interp() to get x and y arrays and a z matrix that can be fed to contour().
> This is the command: interp(x,y,z,extrap=F,linear=FALSE,duplicate='mean')
> In the result there are, consistently, some discontinuities. This happens always in the 'middle' of the data.
> I've uploaded a plot that might clarify the problem:
> As you can see the middle of the plot is discontinue. When I look at the data, there is no particular reason why this should happen.
> The problem seems to be a single row in the z matrix returned by interp() right in the middle of the matrix (line 30 of 60). Replacing this line with the mean of row 29 and 31 seems to solve the problem. This results in this plot: This works, but it is not nice of course.
> Is this something that looks familiar to someone? Can I replace the interp() function with something else? Could this be due to the particular way my data is sampled?

I don't know what's going wrong in inter(), but if your original x and y values are already on a grid, you might be better off just massaging the data directly to the form that contour wants. That is, something like this:

xvals <- sort(unique(x))
yvals <- sort(unique(y))
xindex <- match(x, xvals)
yindex <- match(y, yvals)

zarray <- matrix(NA, length(xvals), length(yvals)) zarray[cbind(xindex, yindex)] <- z

contour(xvals, yvals, zarray)

(I haven't tested this code; you didn't give any data to test it on.)

Duncan Murdoch mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Tue 17 Jun 2008 - 14:19:07 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 17 Jun 2008 - 14:30:47 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive