Article Complete
    AIM

    Visualize Data in 3D Using plot3D R-Package

    Visualize Data in 3D Using plot3D R-Package

    Discover how to visualize data in 3D with the plot3D R package. Click to learn practical steps and enhance your data analysis skills today!

    Visualize Data in 3D Using plot3D R-Package
    Nikita Shiledarbaxi··Updated DECEMBER 30, 2024, 5:30 AM
    5 min

    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.

    Your reaction

    Discussion

    What's your take on this story?

    0/1000

    No comments yet — be the first to share your view.

    Explore our newsletters

    Build your routine with some of our top newsletters or view them all here.

    Wake up informed

    Make sense of the day's AI news and breakthroughs with our morning briefing.

    Belamy
    WeeklyBelamySee the latest

    Industry intelligence

    Receive a roundup of AI adoption stories by industry vertical, curated for professionals.

    3x WeeklySector6See the latest

    By signing up, you agree to our Privacy Policy.

    Membership

    Stay ahead with AI insights.

    Unlimited access to premium articles, exclusive analysis, and the stories shaping India's AI economy.

    Subscribe now

    AIM Americas

    1190 Miraloma Way Suite P, Sunnyvale, CA 94085

    AIM India

    1st Floor, Sakti Statesman, Bellandur, Bengaluru – 560103

    © 2026 AIM. All rights reserved.