Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (2024)

  • Install and load required packages
  • Prepare the data
  • The function scatter3d
  • Basic 3D scatter plots
  • Plot the points by groups
    • Default plot
    • Remove the surfaces
    • Add concentration ellipsoids
    • Change point colors by groups
  • Axes
    • Change axis labels:
    • Remove axis scales
    • Change axis colors
  • Add text labels for the points
  • Export images
  • See also
  • Infos

I recently posted an article describing how to make easily a 3D scatter plot in R using the package scatterplot3d.

This R tutorial describes how to perform an interactive 3d graphics using R software and the function scatter3d from the package car.

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

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (1)

The packages rgl and car are required for this tutorial:

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

Note that, on Linux operating system, the rgl package can be installed as follow:

sudo apt-get install r-cran-rgl

Load the packages:

library("car")

We’ll use the iris data set in the following examples :

data(iris)head(iris)
 Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 4.7 3.2 1.3 0.2 setosa4 4.6 3.1 1.5 0.2 setosa5 5.0 3.6 1.4 0.2 setosa6 5.4 3.9 1.7 0.4 setosa
sep.l <- iris$Sepal.Lengthsep.w <- iris$Sepal.Widthpet.l <- iris$Petal.Length

iris data set gives the measurements of the variables sepal length and width, petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica.

The simplified formats are:

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

  • x, y, z are respectively the coordinates of points to be plotted. The arguments y and z can be optional depending on the structure of x.
  • formula: a model formula of form y ~ x + z. If you want to plot the points by groups, you can use y ~ x + z | g where g is a factor dividing the data into groups
  • data: data frame within which to evaluate the formula
library(car)# 3D plot with the regression planescatter3d(x = sep.l, y = pet.l, z = sep.w)

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (2)

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

Change point colors and remove the regression surface:

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

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (3)

Default plot

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

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (4)

Remove the surfaces

  • To remove the grids only, the argument grid = FALSE can be used as follow:
scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species, grid = FALSE)

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (5)

Note that, the display of the surface(s) can be changed using the argument fit. Possible values for fit are “linear”, “quadratic”, “smooth” and “additive”

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

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (6)

  • Remove surfaces. The argument surface = FALSE is used.
scatter3d(x = sep.l, y = pet.l, z = sep.w, groups = iris$Species, grid = FALSE, surface = FALSE)

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (7)

Add concentration ellipsoids

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

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (8)

Remove the grids from the ellipsoids:

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

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (9)

Change point colors by groups

The argument surface.col is used. surface.col is a vector of colors for the regression planes.

For multi-group plots, the colors are used for the regression surfaces and for the points in the several groups.

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

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (10)

Read more about colors in R: colors in R

It’s also possible to use color palettes from the RColorBrewer package:

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)

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (11)

Change axis labels:

The arguments xlab, ylab and zlab are used:

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

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (12)

Remove axis scales

axis.scales = FALSE

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

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (13)

Change axis colors

By default, different colors are used for the 3 axes. The argument axis.col is used to specify colors for the 3 axes:

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

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (14)

The arguments below are used:

  • labels: text labels for the points, one for each point
  • id.n: Number of relatively extreme points to identify automatically
scatter3d(x = sep.l, y = pet.l, z = sep.w, surface=FALSE, labels = rownames(iris), id.n=nrow(iris))

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (15)

The plot can be saved as png or pdf.

  • The function rgl.snapshot() is used to save the screenshot as png file:
rgl.snapshot(filename = "plot.png")
  • The function rgl.postscript() is used to saves the screenshot to a file in ps, eps, tex, pdf, svg or pgf format:
rgl.postscript("plot.pdf",fmt="pdf")

The function Identify3d()[ car package] allows to label points interactively with the mouse.

This analysis has been performed using R software (ver. 3.1.2) and car (ver. 2.0-25)

Enjoyed this article? I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter, Facebook or Linked In.

Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!

Avez vous aimé cet article? Je vous serais très reconnaissant si vous aidiez à sa diffusion en l'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!

Amazing interactive 3D scatter plots - R software and data visualization - Easy Guides - Wiki (2024)
Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 5691

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.