# Creating surface plots

May 28, 2010
By

(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 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

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 by Casper

The lattice graphics package has a function wireframe and we use the data in the object elevation.fit 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 = elevation.fit, 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

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.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...