Introduction:

In this vignette I’m going to give a brief introduction to the persp3d and plot3d functions within the rgl package. These two functions give you the ability to create Interactive 3-D visualizations of data.3-D models are useful for plotting data sets with two or more predictive variables. rgl is a 3-D graphics package that produces real-time interactive 3D plots.The rgl package can be used to zoom the graphics,interactively rotate and select regions within data sets.

To work with the persp3d and plot3d functions you’ll first need to install the rgl package.

Install the rgl package:

install.packages("rgl")

Load the rgl package:

library.("rgl")

plot3d function:

To demonstrate the functionality of plot3d We’ll use the iris data set below:

data(iris)
head(iris)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa

The plot3d function plots points within the interactive rgl window in R. Unfortunately because it is an interactive graph It can’t be knitted to the published PDF. Plot3d is similar to the classic plot function, but works in 3 dimensions.A 3D scatterplot can be used to see how a response variable relates to two predictor variables. A 3D scatterplot is a three-dimensional graph that is useful for investigating desirable response values and operating conditions.

View(iris)
with(iris,plot3d(Sepal.Length, Sepal.Width, Petal.Length))

You can use your mouse to manipulate, resize and change the perspective of the plot.

persp3d function:

For the persp3d function I will be using the volcano data set which comes with the rgl package.

The persp3d function can be used to draw surfaces. It is similar to the classic persp function, but with greater flexibility. Any of x, y or z can be specified using matrices, not just z. This allows parametric surfaces to be plotted. An even simpler specification is possible: x may be a function, in which case persp3d will work out the grid itself.

persp3d(volcano,col="red")

Conclusion

This is a brief introduction to two functions within the rgl package. Aside from the functions discussed above there is a huge range of additional functionality to the rgl package which is a great way to create Interactive 3-D visualisations of data.

References

  1. https://cran.r-project.org/web/packages/rgl/index.html
  2. https://www.rdocumentation.org/packages/rgl/versions/0.100.19/topics/persp3d
  3. http://www.sthda.com/english/wiki/a-complete-guide-to-3d-visualization-device-system-in-r-r-software-and-data-visualization#load-the-rgl-package
  4. https://www.rdocumentation.org/packages/rgl/versions/0.100.19/topics/plot3d