# [R] Contour Plots

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Jul 11 12:47:16 CEST 2003

```Martin Maechler <maechler at stat.math.ethz.ch> writes:

> >>>>> "Riley" == Riley Metzger <alpha_white_wolf at hotmail.com>
> >>>>>     on Thu, 10 Jul 2003 13:53:56 -0400 writes:
>
>     Riley> Hello, I'm a grad. student in statistics and am
>     Riley> looking for some information on how R draws its
>     Riley> contours.  I suspect you are using a Bezier spline.
>
> I don't know if what we do can be called a Bezier spline (it
> would be the most simple, i.e. "linear" one...), but in any
> case, we simply do linear interpolation :
>
> contour() uses data on a grid.
> The only thing you have to do is drawing line segments in
> rectangles. In the following case, given the z-values in points
> A & B, find the "*" intersection on the AB line
> A & C, find the "+" intersection on the AC line
> now draw the segment from "*" to "+"
>
>   A ____*____ B
>    |   .     |
>    |  .      |
>    | .       |
>    |.        |
>    +         |
>    |_________|
>   C           D
>
> (and all you guys with silly proportional fonts in your e-mail
>  reader will not see a nice picture above ....)

The actual source is available... (the contourLines function in
src/main/plot3d.c). It's a little more elaborate than Martin says
because there can be intersections on all four sides, in which case
you have to decide whether to connect them like

+--+      +--+
|/ |  or  | \|
| /|      |\ |
+--+      +--+

(in principle, they could - and sometimes theoretically should - also
cross, but I don't think we allow them to do that).

There are more elaborate algorithms in existence, but from what I
gather, they tend to be sensitive to the smoothness of the surface and
can give wild results occasionally (e.g. have contour lines for
different levels intersect eachother).

--
O__  ---- Peter Dalgaard             Blegdamsvej 3
c/ /'_ --- Dept. of Biostatistics     2200 Cph. N
(*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907

```