[R] interp() function output not continue

Duncan Murdoch murdoch at stats.uwo.ca
Tue Jun 17 16:03:37 CEST 2008


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: http://examples.attic.sent.com/example.png
> 
> 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: http://examples.attic.sent.com/example_fix.png. 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



More information about the R-help mailing list