The Art of R Programming

(WallPaper) #1

12.4 Creating Three-Dimensional Plots.............................................


R offers a number of functions to plot data in three dimensions such as
persp()andwireframe(), which draw surfaces, andcloud(), which draws three-
dimensional scatter plots. Here, we’ll look at a simple example that uses
wireframe().

> library(lattice)
> a <- 1:10
> b <- 1:15
> eg <- expand.grid(x=a,y=b)
> eg$z <- eg$x^2 + eg$x*eg$y
> wireframe(z ~ x+y, eg)

First, we load thelatticelibrary. Then the call toexpand.grid()creates
a data frame, consisting of two columns namedxandy, in all possible com-
binations of the values of the two inputs. Here,aandbhad 10 and 15 val-
ues, respectively, so the resulting data frame will have 150 rows. (Note that
the data frame that is input towireframe()does not need to be created by
expand.grid().)
We then added a third column, namedz, as a function of the first two
columns. Our call towireframe()creates the graph. The arguments, given
in regression model form, specify thatzis to be graphed againstxandy.Of
course,z,x, andyrefer to names of columns ineg. The result is shown in
Figure 12-13.

Figure 12-13: Example of usingwireframe()

282 Chapter 12

Free download pdf