How to create 3D Plots and Animations in R using rgl Package
Last Updated :
24 Apr, 2025
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
install.packages ( "rgl" )
library (rgl)
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)))
persp3d (x, y, z, col = "blue" )
play3d ( spin3d (axis = c (0, 0, 1)), duration = 10)
|
Output:

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
install.packages ( "rgl" )
library (rgl)
data (iris)
plot3d (iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length, col = "red" , size = 3, type = "s" )
play3d ( spin3d (axis = c (0, 0, 1)), duration = 10)
|
Output:

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
library ( "rgl" )
data (iris)
bg3d (color = "grey" )
points3d (iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length, col = "black" )
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" )
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
library ( "rgl" )
data (iris)
bg3d (color = "lightgreen" )
plot3d (iris$Sepal.Length, iris$Sepal.Width, iris$Petal.Length, col = "yellow" , type = "l" )
bbox3d (col = "blue" )
play3d ( spin3d (axis = c (0, 1, 0)), duration = 10)
|
Output:

Bounding Box 3D graph
Similar Reads
How to Create an Animated Line Graph using Plotly
An animated line graph is a visual representation of data that changes over time or over a categorical variable. It can be a powerful tool for visualizing trends and patterns in data and can help to communicate complex ideas in a clear and concise way. In this tutorial, we will learn how to create a
5 min read
How to create a scatter plot using lattice package in R?
In this article, we will discuss how to create the scatter plots using lattice package in R programming language. In R programming, the Lattice package is a data visualization library that consists of various functions to plot different kinds of plots. Using the lattice library we can able to plot v
2 min read
How to Plot in 3D clusters using plotly package
R-Language is widely used in Data Science, Data Visualization Data Analysis many more, etc. Plotly package is highly rich in plotting various graphs, These graphs/charts are highly interactive and user-friendly. The 3D cluster visualizes the similarity between variables as 3-D spatial relationships.
2 min read
How to create a boxplots using lattice package in R?
In this article, we will discuss how to create the boxplots using lattice package in R Programming language. In R programming, Lattice package is a data visualization library and consists of various functions to plot different kind of plots. Using lattice library we can able to plot different plots
2 min read
How to create 3D plot for iris flower dataset with rgl package in R?
In R programming, the rgl() package is used to visualize the datasets in 3-Dimensions. It contains many 3D plotting functions, among them most widely functions used to plot 3d graphs include plot3d(), bgplot3d(), open3d(), planes3d(), points3d() etc. Each package mentioned here have specific usage b
3 min read
How to Plot 3D Scatter Diagram Using ggplot in R
The ggplot2 package in R is one of the most popular tools for creating complex and aesthetically pleasing plots. However, ggplot2 is primarily designed for 2D plotting, which presents a challenge when it comes to creating 3D scatter plots. While ggplot2 does not natively support 3D plotting, it can
4 min read
How to Create Grouped Line Chart Using ggplot and plotly in R
Creating grouped line charts in R allows you to visualize multiple trends or series in the same plot. By using the combination of ggplot2 plotting and plotly for interactivity, you can create rich, dynamic visualizations that let you explore your data in depth. In this article, we will explore how t
4 min read
How to do 3D line plots grouped by two factors with the Plotly package in R?
Plotly is a powerful library for making interactive charts. We will be able to plot 3D line plots using Plotly that can be utilized to analyze complex data and display results in a clear manner. Syntax: plot_ly(data, x = ~x, y = ~y, z = ~z, type = "scatter3d", mode = "lines") Parameters: data: The d
3 min read
Draw Composition of Plots Using the patchwork Package in R
In this article, we will discuss how to draw a composition of plots using the patchwork package in R programming language. The patchwork package makes it easier to plot different ggplots in a single graph. We will require both ggplot2 packages for plotting our data and the patchwork package to combi
4 min read
How to plot user-defined functions in R?
Plotting user-defined functions in R is a common task for visualizing mathematical functions, statistical models, or custom data transformations. This article provides a comprehensive guide on how to plot user-defined functions in R, including creating simple plots, enhancing them with additional fe
3 min read