Three-Dimensional Charting in R

Creating 3D charts is a powerful capability of R. These charts are particularly interesting because they are interactive. The viewing angle can be manipulated.

This is not as challenging as it sounds. Here is one way to accomplish this.

First, the basic code to open packages and set things up.

data(iris3, package = 'datasets')
iris3 <- as.data.frame(iris3)
names(iris3)<- make.names(names(iris3))
library(nlme)
library(mgcv)
## This is mgcv 1.8-9. For overview type 'help("mgcv-package")'.
library(rgl, pos = 4)
library(mgcv, pos = 4)
library(car)

Now for the creation of the charts. Note the code calls for the chart to be opened in a separate window. Also note the code creates charts one at at time.

The first chart plots petal and sepal characteristics of the Setosa variety:

scatter3d(iris3$Petal.W..Setosa, iris3$Petal.L..Setosa, iris3$Sepal.L..Setosa, fit = 'linear', residuals = TRUE, bg = 'white', axis.scales = TRUE, grid = TRUE, ellipsoid = FALSE, xlab = 'Petal.W..Setosa', ylab = 'Petal.L..Setosa', zlab = 'Sepal.L..Setosa')

The second chart plots petal lengths for all three varieties.

scatter3d(iris3$Petal.L..Versicolor, iris3$Petal.L..Setosa, iris3$Petal.L..Virginica, fit = 'linear', residuals = TRUE, bg = 'white', axis.scales = TRUE, grid = TRUE, ellipsoid = FALSE, xlab = 'Petal.L..Versicolor', ylab = 'Petal.L..Setosa', zlab = 'Petal.L..Verginica')

This last code chunk stores the chart as a PNG file on your computer.

rgl.snapshot("C:/Documents and Settings/abc/Desktop/RGLGraph.png")
## Warning in rgl.snapshot("C:/Documents and Settings/abc/Desktop/
## RGLGraph.png"): RGL: Pixmap save: unable to open file 'C:\Documents and
## Settings\abc\Desktop\RGLGraph.png' for writing
## Warning in rgl.snapshot("C:/Documents and Settings/abc/Desktop/
## RGLGraph.png"): 'rgl.snapshot' failed

This code was adapted from “Creating 3D Graphs from Data in R”