## Make a 2D surface plot

You want to create a 2D contour plot of a surface

**Solution:**

R has several built-in functions that produce two-dimensional plots of
a surface. The `contour`

function displays isolines of a matrix z, where
the elements of z are interpreted as heights with respect to the xy
plane.

The following code makes a contour plot for the function sin(x*y) evaluated over the rectangle [0, 3]x[0, 4].

```
> x <- seq(0, 3, .2) # Set grid points for x
> y <- seq(0, 4, .1) # Set grid points for y
> z <- outer(x, y, FUN=function(xx,yy) { sin(xx*yy) }) # Compute function at all grid points
> contour(x, y, z) # Make contour plot
```

The result can be seen in the Figure below
See rule 4.12 in *The R Primer* for more
information. The book also shows how to use the
`filled.contour`

or `image`

functions to make improved 2D images.

Back to tips.