Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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) 
col <- colorlut[ z-zlim[1]+1 ] 

rgl.open()
rgl.surface(x, y, z, color=col, back="lines")

How it works...

RGL is a three-dimensional real-time rendering device driver system for R. We used the rgl.surface() function...