Customizing Animated and 3D Plots using RGL Package

The RGL Package in R also provides us with various functionalities to customize our 3D plots. We can change the size, color, and shape of the points plotted on the graph, as well as add a bounding box to it. Let us see a couple of examples to customize the plots.

Adding axis lines

In this example, we customized out the plot by adding a background color to it using the bg3d() function. We also used the Points3d() function to plot the points on the graph and the lines3d() function to add x, y, and z-axis lines and provide them with different colors. Lastly, we added an animation to the graph using the play3d() function which rotates the graph on the x-axis.

R




# import rgl package
library("rgl")
  
# load the dataset
data(iris)
  
# define background color
bg3d(color = "grey")
  
# plot the points
points3d(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length, col ="black")
  
# define the axis lines
lines3d(c(min(iris$Sepal.Length), max(iris$Sepal.Length)), c(0,0), c(0,0), col = "green")
lines3d(c(0,0), c(min(iris$Sepal.Width), max(iris$Sepal.Width)), c(0,0), col = "blue")
lines3d(c(0,0), c(0,0), c(min(iris$Petal.Length), max(iris$Petal.Length)), col = "yellow")
  
# add animation
play3d(spin3d(axis = c(1, 0, 0)), duration = 10)


Output:

Adding axis to 3D plot

Adding Bounding Box

In this example, we plot the lines instead of points or spheres using the plot3d() function with the type argument as ‘l’ which indicates lines. The bbox() function is used to add the bounding box to the graph and we provided the col to blue as the arguments which will be the color of our bounding box.

R




# loading RGL Package
library("rgl")
  
# loading iris dataset
data(iris)
  
# adding background color
bg3d(color = "lightgreen")
  
# ploting lines
plot3d(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length, col ="yellow", type = "l")
  
# adding bounding box
bbox3d(col = "blue")
  
#adding animations
play3d(spin3d(axis = c(0, 1, 0)), duration = 10)


Output:

Bounding Box 3D graph



How to create 3D Plots and Animations in R using rgl Package

The R programming language provides a powerful set of tools and packages for creating interactive 3D plots and animations. One such package is RGL. In this article, let us learn How to create 3D plots and animations using the RGL package.

Similar Reads

Create 3D Plots and Animation in R using RGL Package

The RGL package in R provides an interface to the OpenGL graphics library for creating interactive 3D plots and animations. It is built on top of OpenGL, the popular 3D graphics library that is widely used in computer graphics and game development. Using this package, we can provide animations to the 3D graphs. We can use Rgl Package to rotate the graph on its different axis....

Examples to Create 3D Plots and Animation in R

Now let us see a few examples to understand the concept....

Customizing Animated and 3D Plots using RGL Package

...

Contact Us