MITB Banner

Visualize Data in 3D Using plot3D R-Package

Share

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.
 install.packages("plot3D")
 library("plot3D") 
  1. Load the built-in Iris dataset.

data(iris)

Display initial records of the data.

head(iris)

  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.

Output:

  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)")

Output:

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") 

Output:

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.

Output:

  • 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) 

Output:

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

Output:

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

Output:

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

Load the dataset.

data(mtcars)

Display its initial records.

head(mtcars)

Output:

 # 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") 

    Output:

References

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

Share
Picture of Nikita Shiledarbaxi

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.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Courses & Careers

Become a Certified Generative AI Engineer

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.