Visualize Data in 3D Using plot3D R-Package

R 3D visualization

Data visualization holds an important place among a wide range of tasks handled by data scientists and analysts for getting a proper insight of the data in hand. This post will talk about how to visualize data in 3D using the plot3D package of the R programming language.

Note: The code snippets throughout this article have been implemented using RStudio IDE (version 1.2.1335). RStudio can be downloaded from here

Practical implementation

  1. Install and load plot3D package.
  1. Load the built-in Iris dataset.



Sign up for your weekly dose of what's up in emerging technology.

Display initial records of the data.


  1. Extract sepal length, petal length and sepal width data from the respective columns of the dataset. The values of these attributes for a given record will be used as x,y and z coordinates for plotting a point in 3D.
 a <- sepal.len <- iris$Sepal.Length
 b <- petal.len <- iris$Petal.Length
 c <- sepal.width <- iris$Sepal.Width 
  • Scatter plots:
  1. scatter3D() function can be used to draw 3-dimensional scatter plot.

scatter3D(a,b,c, clab = c("Sepal", "Width (cm)"))

Here, a,b and c represent the x,y and z coordinates of points respectively. ‘Clab’ 

parameter specifies the label of the color key.


  1. The type of box to be drawn surrounding scatter plot points can be specified using ‘bty’ parameter. The color key legend can be disabled using ‘colkey’ parameter.

scatter3D(a, b, c, bty = "f", colkey = FALSE, main ="bty= 'f' (full box)")


scatter3D(a, b, c, bty = "b2", colkey = FALSE, main ="bty= 'b2'(with back panels and grid line)")

  1. By specifying ‘bty’ value to ‘u’, we can manually select attributes such col.panel, col.grid etc.
 scatter3D(a, b, c, pch = 18, bty = "u", colkey = FALSE, 
           main ="bty= 'u'", col.panel ="steelblue", expand =0.4, 
           col.grid = "darkblue") 


6) The plotted points can be annotated using text3D() function.

 scatter3D(a, b, c, pch = 18,  theta = 30, phi = 30, main = "IRIS Flower data", xlab = 
 "Sepal.Length", ylab ="Petal.Length", zlab = "Sepal.Width") 

‘theta’ and ‘phi’ parameters define the azimuthal angle, and co-altitude of the scatter plot.


  • Line plots:
  1. ‘Type’ parameter of the scatter3D() function should be specified for customizations like lines (only), points and lines, vertical lines.
 #Only lines 
 scatter3D(a, b, c, phi = 0, bty = "g", type = "l", ticktype = "detailed", lwd = 4) 


 #Vertical lines
 scatter3D(x, y, z, phi = 0, bty = "g",  type = "h", ticktype = "detailed") 


 #Both points and lines
 scatter3D(x, y, z, phi = 30, bty = "g",  type = "b", ticktype = "detailed") 


  • Regression plot
  1. We will use the built-in mtcars dataset for putting a regression plot.

Load the dataset.


Display its initial records.



 # Initialize x, y, z coordinate variables
 x <- mtcars$wt
 y <- mtcars$disp
 z <- mtcars$mpg
 # Calculate linear regression (z = ax + by + c) using lm() method
 fit <- lm(z ~ x + y)
 # predict values on a X-Y grid
 grid.lines = 30   #number of lines on grid
 #predict x, y and z variables’ values
 x.pred <- seq(min(x), max(x), length.out = grid.lines)
 y.pred <- seq(min(y), max(y), length.out = grid.lines)
 xy <- expand.grid( x = x.pred, y = y.pred)
 z.pred <- matrix(predict(fit, newdata = xy), 
                  nrow = grid.lines, ncol = grid.lines)
 # fit data points to drop lines perpendicular to the grid surface
 fitpoints <- predict(fit)
 # draw scatter plot with regression plane
 scatter3D(x, y, z, pch = 18, cex = 2, 
           theta = 20, phi = 20, ticktype = "detailed",
           xlab = "wt", ylab = "disp", zlab = "mpg",  
           surf = list(x = x.pred, y = y.pred, z = z.pred,  
                       facets = NA, fit = fitpoints), main = "mtcars") 



The following article throws light on to visualize data in 3D using the plot3D package of R programming language.

More Great AIM Stories

Nikita Shiledarbaxi
A zealous learner aspiring to advance in the domain of AI/ML. Eager to grasp emerging techniques to get insights from data and hence explore realistic Data Science applications as well.

Our Upcoming Events

Conference, in-person (Bangalore)
Machine Learning Developers Summit (MLDS) 2023
19-20th Jan, 2023

Conference, in-person (Bangalore)
Rising 2023 | Women in Tech Conference
16-17th Mar, 2023

Conference, in-person (Bangalore)
Data Engineering Summit (DES) 2023
27-28th Apr, 2023

Conference, in-person (Bangalore)
MachineCon 2023
23rd Jun, 2023

Conference, in-person (Bangalore)
Cypher 2023
20-22nd Sep, 2023

3 Ways to Join our Community

Whatsapp group

Discover special offers, top stories, upcoming events, and more.

Discord Server

Stay Connected with a larger ecosystem of data science and ML Professionals

Subscribe to our newsletter

Get the latest updates from AIM