Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Get the first value in each group in R
    Get the first value in each group in R? R
  • Add Significance Level and Stars to Plot in R
    Add Significance Level and Stars to Plot in R R
  • pheatmap function in R
    The pheatmap function in R R
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R R
  • Error in rbind(deparse.level ...) numbers of columns of arguments do not match
    Error in rbind(deparse.level …) numbers of columns of arguments do not match R
  • Gamma distribution in R
    Gamma distribution in R R
  • Data Scientist in 2023
    How to Become a Data Scientist in 2023 Machine Learning
  • Top Reasons To Learn R
    Top Reasons To Learn R in 2023 Machine Learning
Interactive 3d plot in R

Interactive 3d plot in R-Quick Guide

Posted on May 31May 31 By Jim No Comments on Interactive 3d plot in R-Quick Guide
Tweet
Share
Share
Pin

Interactive 3d plot in R, This R lesson shows how to create dynamic 3d graphics with R and the scatter3d function from the package car.

The rgl package is used by the scatter3d() function to draw and animate 3D scatter plots.

Install and load all necessary packages.

For this tutorial, you’ll need the rgl and car packages.

Two Sample Proportions test in R-Complete Guide – Data Science Tutorials

install.packages(c("rgl", "car"))

Load the packages:

library("car")
library("rgl")

In the following cases, we’ll use the iris data set.

data(iris)
head(iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa
sep.l <- iris$Sepal.Length
sep.w <- iris$Sepal.Width
pet.l <- iris$Petal.Length

The iris data set contains measurements of the variables sepal length and breadth, as well as petal length and width, for 50 blooms from three different iris species. Iris setosa, versicolor, and virginica are the three species.

How to perform the MANOVA test in R? – Data Science Tutorials

The following are the simpler formats.

scatter3d(formula, data)
scatter3d(x, y, z)

The coordinates of the points to be plotted are x, y, and z, respectively. Depending on the structure of x, the variables y and z may be optional. formula: a model formula of the form y ~ x + z.

If you want to visualize the points by groups, use y ~ x + z | g, where g is a factor that divides the data into groups data: data frame to test the formula within

Example:

library(car)

3D plot with the regression plane

scatter3d(x = sep.l, y = pet.l, z = sep.w)

It’s worth noting that the plot can be manually rotated by pressing and holding the mouse or touchpad. It can also be zoomed by using the mouse scroll wheel, pressing ctrl + on the touchpad on a PC, or two fingers (up or down) on a Mac.

Control Chart in Quality Control-Quick Guide – Data Science Tutorials

Remove the regression surface and change the point colors:

scatter3d(x = sep.l, y = pet.l, z = sep.w,
        point.col = "blue", surface=FALSE)

Plot the points by groups

scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species)

Surfaces removal.

To delete simply the grids, use the grid = FALSE option as follows:

scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,
          grid = FALSE)

The fit argument can be used to adjust the display of the surface(s). “Linear,” “quadratic,” “smooth,” and “additive” are all possible fit values.

scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,
          grid = FALSE, fit = "smooth")

Surfaces should be removed. The surface = FALSE option is utilized.

scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,
          grid = FALSE, surface = FALSE)

Concentration ellipsoids are added.

Detecting and Dealing with Outliers: First Step – Data Science Tutorials

scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,
          surface=FALSE, ellipsoid = TRUE)

Remove the ellipsoids’ grids as follows.

scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,
          surface=FALSE, grid = FALSE, ellipsoid = TRUE)

Change the color of the points in groups.

The surface.col argument is used. The color vector for the regression planes is surface.col.

Colors are utilized for the regression surfaces and the points in the various groups in multi-group displays.

scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,
          surface=FALSE, grid = FALSE, ellipsoid = TRUE,
          surface.col = c("#999999", "#E69F00", "#56B4E9"))

Color palettes from the RColorBrewer package can also be used:

library("RColorBrewer")
colors <- brewer.pal(n=3, name="Dark2")
scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,
          surface=FALSE, grid = FALSE, ellipsoid = TRUE,
          surface.col = colors)

Change the labels on the axes

Checking Missing Values in R – Data Science Tutorials

The following arguments are used: xlab, ylab, and zlab

scatter3d(x = sep.l, y = pet.l, z = sep.w,
        point.col = "blue", surface=FALSE,
        xlab = "Sepal Length (cm)", ylab = "Petal Length (cm)",
        zlab = "Sepal Width (cm)")

Remove axis scales: axis.scales = FALSE

scatter3d(x = sep.l, y = pet.l, z = sep.w,
        point.col = "blue", surface=FALSE,
        axis.scales = FALSE)

Axis colors can be changed.

The three axes are colored differently by default. Colors for the three axes are specified using the axis.col argument:

scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species,
          surface=FALSE, grid = FALSE, ellipsoid = TRUE,
          axis.col = c("black", "black", "black"))

Label the points with text labels.

The following arguments are used:

labels: one for each point, each with its own text label.

Best GGPlot Themes You Should Know – Data Science Tutorials

id.n: The number of somewhat extreme points that must be automatically identified.

scatter3d(x = sep.l, y = pet.l, z = sep.w,
          surface=FALSE, labels = rownames(iris), id.n=nrow(iris))

Images should be exported

The plot can be saved in either png or pdf format.

The screenshot is saved as a png file using the function rgl.snapshot():

rgl.snapshot(filename = "plot.png")

The rgl.postscript() function is used to save the screenshot as a ps, eps, tex, pdf, svg, or pgf file:

rgl.postscript("plot.pdf",fmt="pdf")
Tweet
Share
Share
Pin
R

Post navigation

Previous Post: How to Perform a Log Rank Test in R
Next Post: Arrange Data by Month in R with example

Related Posts

  • What Is the Best Way to Filter by Date in R
    What Is the Best Way to Filter by Date in R? R
  • How to Replace Inf Values with NA in R
    How to Replace Inf Values with NA in R R
  • How to Get a Job as a Data Engineer
    How to Get a Job as a Data Engineer? R
  • Random Forest Machine Learning
    Random Forest Machine Learning Introduction R
  • Arrange the rows in a specific sequence in R
    Arrange the rows in a specific sequence in R R
  • How to Find Optimal Clusters in R, K-means clustering is one of the most widely used clustering techniques in machine learning.
    How to Find Optimal Clusters in R? R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • How do augmented analytics work
    How do augmented analytics work? R
  • Methods for Integrating R and Hadoop
    Methods for Integrating R and Hadoop complete Guide R
  • Tips for Rearranging Columns in R
    Tips for Rearranging Columns in R R
  • How to Replace String in Column in R
    How to Replace String in Column using R R
  • Error in sum(List) : invalid 'type' (list) of argument
    Error in sum(List) : invalid ‘type’ (list) of argument R
  • How to use image function in R
    How to use the image function in R R
  • Ad Hoc Analysis
    What is Ad Hoc Analysis? Statistics
  • Bind together two data frames by their rows or columns in R
    Bind together two data frames by their rows or columns in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme