Now Reading
Visualize Data in 3D Using plot3D R-Package

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

Register for our Workshop>>

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:

See Also
Pygal Visualization Tutorial

 #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.

What Do You Think?

Join Our Telegram Group. Be part of an engaging online community. Join Here.

Subscribe to our Newsletter

Get the latest updates and relevant offers by sharing your email.

Copyright Analytics India Magazine Pvt Ltd

Scroll To Top