Creating three-dimensional surface plots

In this recipe, we will use a special library to make a three-dimensional surface plot for the volcano dataset. The resulting plot will also be interactive so that we can rotate the visualization using a mouse to look at it from different angles.

Getting ready

For this recipe, we will use the rgl package, so we must first install and load it:

install.packages("rgl")
library(rgl)

We will only use the inbuilt volcano dataset, so we need not load any other dataset.

How to do it...

Let's make a simple three-dimensional surface plot that shows the terrain of the Maunga Whau volcano:

z <- 2 * volcano x <- 10 * (1:nrow(z)) y <- 10 * (1:ncol(z)) zlim <- range(z) zlen <- zlim[2] - zlim[1] + 1 colorlut <- terrain.colors(zlen) ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.