QUESTION: “How do I make a 3D scatterplot in R?”

Data

We’ll use the “palmerpenguins” packages (https://allisonhorst.github.io/palmerpenguins/) to address this question. You’ll need to install the package with install.packages(“palmerpenguins”) if you have not done so before, call library(“palmerpenguins”), and load the data with data(penguins)

Preliminaries

Download packages

Only do this once, then comment out of the script.

#install.packages("palmerpenguins")
#install.packages("ggpubr")

Load the libraries

library(ggpubr)
## Loading required package: ggplot2
library(palmerpenguins)
## Warning: package 'palmerpenguins' was built under R version 4.1.2
library(scatterplot3d)

Load the data

data(penguins)

Making a 3d scatterplot using scatterplot3d() function.

# Can make vectors to store columns info
bill_length_mm <- penguins$bill_length_mm
bill_depth_mm <- penguins$bill_depth_mm
flipper_length_mm <- penguins$flipper_length_mm

Plotting 3D scatterplot

plot1 <- scatterplot3d(x = bill_length_mm, 
                       y = bill_depth_mm, 
                       z = flipper_length_mm,
              color = 2, 
              pch = 16, type = "h", highlight.3d = TRUE,
              main = "Penguins dataset")
## Warning in scatterplot3d(x = bill_length_mm, y = bill_depth_mm, z =
## flipper_length_mm, : color is ignored when highlight.3d = TRUE

Additional Reading

For more information on this topic, see: https://r-lang.com/how-to-create-scatterplot-in-r-with-example/

Keywords: 1. scatterplot3d() 2. ggpubr 3. palmerpenguins 4. highlight.3d =…