Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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.

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.

The first step is to install the RGL package in R, which can be done by writing the following code:

install.packages("rgl")

Steps to Create 3D Animated Plots

Let us see step by step, how we can create a 3D plot in R and also provide animation to it using the RGL package.

Step 1: Install and load the Rgl package

First, we will install and then load the RGL package in our R script.

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

Step 2: Create a data frame 

Create the dataframe that contains the variables to be plotted on a 3D graph. We can also use R’s inbuilt datagrams such as iris, mtcars, etc

data <- data.frame(x = c(1, 2, 3), y = c(4, 5, 6), z = c(7, 8, 9))

Step 3: Create a 3D scatter plot

After loading the dataframe, we will plot the graph using the plot3d() function and pass the attributes that we want to plot on the graph as parameters.

plot3d(data$x, data$y, data$z)

Step 4: Add labels to the axes

To make the graph more readable and understandable, we can label the axis using the axes3d() function, which takes a list of labels as the parameters.

axes3d(labels = c("X-axis", "Y-axis", "Z-axis"))

Step 5: Customize the graph

We can also customize the graph with various options such as colors, point sizes, and shapes to make it more representative.

plot3d(data$x, data$y, data$z, col = "red", size = 3, type = "s")

Step 6: Create animations

The final step would be to add animations to our graph using the play3d() function, which takes the spin3d() to specify on which axis the graph should rotate and the duration of the animation as the parameters. The default value of spin3d axis is c(0, 0, 1) which will spin the plot on z axis.

play3d(spin3d(axis = c(0, 0, 1), rpm = 10), duration = 10)

Examples to Create 3D Plots and Animation in R

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

Perspective Plot

In this example, we first generated some data and then plot it on the graphs using the persp3d() method, which takes the x, y, and z coordinates and a ‘col’ argument that defines the color of the graph.

R




# import and load rgl package
install.packages("rgl")
library(rgl)
  
# Generate some sample data
x <- seq(-5, 6, by = 0.1)
y <- seq(-5, 7, by = 0.1)
z <- outer(x, y, function(x, y) dnorm(sqrt(x^2 + y^2)))
  
# Create a 3D surface plot
persp3d(x, y, z, col = "blue")
             
# add animation
play3d(spin3d(axis = c(0, 0, 1)), duration = 10)


Output:

3D and animated Perspective Plot

3D and animated Perspective Plot

Scatter Plot

In this example, we take R’s inbuilt dataset ‘iris’ as the input and then plot it on the graphs using the plot3d() method, which is used to plot the scatterplots. It takes the Sapel Length, Sapel Width, and Petal Length as the x, y, and z coordinates respectively, a ‘col’ argument that defines the color of the graph, size of the points of the scatterplots, and type as ‘s’ which means spheres.

R




# import and load rgl package
install.packages("rgl")
library(rgl)
  
# load the set
data(iris)
  
# plot the 3D graph
plot3d(iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length, col = "red", size = 3, type = "s")
  
# add animations
play3d(spin3d(axis = c(0, 0, 1)), duration = 10)


Output:

3D and animated Scatterlot

3D and animated Scatterplot

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:

Adding Bounding Box

Bounding Box 3D graph



Last Updated : 05 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads