Skip to content

Fitting Contour to Data Points

2 messages · Laura Quinn, Gavin Simpson

#
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
Institute of Atmospheric Science
School of Earth and Environment
University of Leeds
Leeds
LS2 9JT

tel: +44 113 343 1596
fax: +44 113 343 6716
mail: laura at env.leeds.ac.uk
#
Laura Quinn wrote:
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