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)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

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.


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)