# Re: [R] Fitting Contour to Data Points

From: Gavin Simpson <gavin.simpson_at_ucl.ac.uk>
Date: Mon 16 May 2005 - 20:41:04 EST

Laura Quinn wrote:
> Apologies for the mass mailing today!
>
> I am attempting to produce a contour plot for phsical data on a map
> matrix. I have a small number of data points which each has an (x,y)
> co-ordinate together with a corresponding value which I would like to
> cvreate a contour plot for.
>
> I have tried the following code:
>
> contour(data\$x,data\$y,data\$value)
>
> but am told:
>
> Error in contour.default(data\$x, data\$y, data\$value) :
> increasing x and y values expected
>
> I have re-ordered the matrix so that the x values are increasing, but
> these do not necessarrily correspond with increasing y values (as this is
> not a regular grid)...can anyone offer a way around this??
>
> Laura Quinn

You are misunderstanding what the x, y and z arguments want. From ?contour we have

```      x,y: locations of grid lines at which the values in 'z' are
measured.  These must be in ascending order.  By default,
equally spaced values from 0 to 1 are used.  If 'x' is a
'list', its components 'x\$x' and 'x\$y' are used for 'x' and
'y', respectively. If the list has component 'z' this is used
for 'z'.

z: a matrix containing the values to be plotted ('NA's are
allowed).  Note that 'x' can be used instead of 'z' for
convenience.

```

So if you have a 10 x 10 grid, you will have 10 values each for x and y and a 10x10 matrix of the 100 points as z. But you don't have a regular grid so you can't use contour directly. Without knowing your data, you'll have to get values on to a regular grid using something like package akima and it's function interp(), then contour the interpolated surface.

There are likely to be other ways. MASS (the book) has an example of using loess() to predict surfaces from irregular data for example...

HTH Gav

```--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpsonATNOSPAMucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help