Creating surface plots

May 28, 2010

[This article was first published on Software for Exploratory Data Analysis and Statistical Modelling, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

A 3d wireframe plot is a type of graph that is used to display a surface – geographic data is an example of where this type of graph would be used or it could be used to display a fitted model with more than one explanatory variable. These plots are related to contour plots which are the two dimensional equivalent.

To illustrate this type of graph we will consider some surface elevation data that is available in the geoR package and was used in the blog post on level plots. The data set in this package is called elevation and stores the elevation height in feet (as multiples of ten feet) for a grid region of x and y coordinates (recorded as multiples of 50 feet). This post has details of the various operations that are undertaken to prepare the data for graphing.

Base Graphics

Fast Tube
Fast Tube by Casper

The function persp is the base graphics function for creating wireframe surface plots. The persp function requires a list of x and y values covering the grid of vertical values which is specified as the z variable. The heights for the display are specified as a table of values which we saved previously as the object z during the calculations when the local trend surface model was fitted to the data. The text on the axis labels are specified by the xlab and ylab function arguments and the main argument determines the overall title for the graph.

persp(seq(10, 300, 5), seq(10, 300, 5), z, phi = 45, theta = 45,
  xlab = "X Coordinate (feet)", ylab = "Y Coordinate (feet)",
  main = "Surface elevation data"

The function arguments phi and theta are used to rotate the viewing angle of the surface. Trial and error is probably the way to go when setting these as good choices depend entirely on the shape of the surface being displayed.

Base Graphics Surface Plot

Base Graphics Surface Plot

The surface is clear and easy to determine the shape and variation in height across the x and y grid coordinates.

Lattice Graphics

Fast Tube
Fast Tube by Casper

The lattice graphics package has a function wireframe and we use the data in the object to create the graph. We use the formula interface to specify first the z axis data (the heights) followed by the two variables specifying the x and y axis coordinates for the data.

wireframe(Height ~ x*y, data =,
  xlab = "X Coordinate (feet)", ylab = "Y Coordinate (feet)",
  main = "Surface elevation data",
  drape = TRUE,
  colorkey = TRUE,
  screen = list(z = -60, x = -60)

The axes labels and title are specified in the same way as the base graphics with the xlab, ylab and main function arguments. A colour key is added using the colorkey function argument and setting it to TRUE.

Lattice Graphics Surface Plot

Lattice Graphics Surface Plot

The surface produced by the wireframe function is similar to the persp function with the main difference between the colours used on the surface.

This blog post is summarised in a pdf leaflet on the Supplementary Material page.

To leave a comment for the author, please follow the link and comment on their blog: Software for Exploratory Data Analysis and Statistical Modelling. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , , , , , , , , , , ,

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)