3D Plots in R
3D Plots in R
3D Plots in R
#IRIS 3D Plot
#install.packages("plot3D")
library("plot3D")
data(iris)
head(iris)
# x, y and z coordinates
x <- sep.l <- iris$Sepal.Length
y <- pet.l <- iris$Petal.Length
z <- sep.w <- iris$Sepal.Width
#Scatter plots
scatter3D(x, y, z, ..., colvar = z, col = NULL, add = FALSE)
text3D(x, y, z, labels, colvar = NULL, add = FALSE)
points3D(x, y, z, ...)
lines3D(x, y, z, ...)
scatter2D(x, y, colvar = NULL, col = NULL, add = FALSE)
text2D(x, y, labels, colvar = NULL, col = NULL, add = FALSE)
#Color palettes
# gg.col: ggplot2 like color
scatter3D(x, y, z, bty = "g", pch = 18, col = gg.col(100))
# ramp.col: custom palettes
scatter3D(x, y, z, bty = "g", pch = 18,
col = ramp.col(c("blue", "yellow", "red")) )
#Change the color by groups
scatter3D(x, y, z, bty = "g", pch = 18,
col.var = as.integer(iris$Species),
col = c("#1B9E77", "#D95F02", "#7570B3"),
pch = 18, ticktype = "detailed",
colkey = list(at = c(2, 3, 4), side = 1,
addlines = TRUE, length = 0.5, width = 0.5,
labels = c("setosa", "versicolor", "virginica")) )
# Regression plane
# The mtcars data will be used:
data(mtcars)
head(mtcars[, 1:6])
# 3D Histogram
# The function hist3D() is used:
hist3D (x, y, z, ..., colvar = z,
col = NULL, add = FALSE)
# fancy 3D histograms
hist3D_fancy<- function(x, y, break.func = c("Sturges", "scott", "FD"), breaks = NULL,
colvar = NULL, col="white", clab=NULL, phi = 5, theta = 25, ...){
scatter3D(x, y,
z = rep(-max(z)/2, length.out = length(x)),
colvar = colvar, col = gg.col(100),
add = TRUE, pch = 18, clab = clab,
colkey = list(length = 0.5, width = 0.5,
dist = 0.05, cex.axis = 0.8, cex.clab = 0.8)
)
par(mar = oldmar)
}
hist3D_fancy(iris$Sepal.Length, iris$Petal.Width,
colvar=as.numeric(iris$Species))
# scatter2D: 2D scatter plot
# Create some data:
# x, y coordinates
set.seed(1234)
x <- sort(rnorm(10))
y <- runif(10)
# Variable for coloring points
col.v <- sqrt(x^2 + y^2)
# text2D
# Only text
with(USArrests, text2D(x = Murder, y = Assault + 5, colvar = Rape,
xlab = "Murder", ylab = "Assault", clab = "Rape",
main = "USA arrests", labels = rownames(USArrests), cex = 0.6,
adj = 0.5, font = 2))
# Other functions
# It’s also possible to draw arrows, segments and rectangles in a 3D or 2D plot using the functions
below:
arrows3D(x0, y0, z0, x1, y1, z1, ..., colvar = NULL,
col = NULL, type = "triangle", add = FALSE)
segments3D(x0, y0, z0, x1, y1, z1, ..., colvar = NULL,
col = NULL, add = "FALSE")
rect3D(x0, y0, y0, x1, y1, z1, ..., colvar = NULL,
col = NULL, add = FALSE)
arrows2D(x0, y0, z0, x1, y1, z1, ..., colvar = NULL,
col = NULL, type = "triangle", add = FALSE)
segments2D(x0, y0, z0, x1, y1, z1, ..., colvar = NULL,
col = NULL, add = "FALSE")
rect2D(x0, y0, y0, x1, y1, z1, ..., colvar = NULL,
col = NULL, add = FALSE)
# Prepare the data: we want to plot 4 arrows starting from the point of coordinates c(x0, y0, z0) and
ending at c(x1, y1, z1)
x0 <- c(0, 0, 0, 0)
y0 <- c(0, 0, 0, 0)
z0 <- c(0, 0, 0, 0)
x1 <- c(0.89, -0.46, 0.99, 0.96)
y1 <- c(0.36, 0.88, 0.02, 0.06)
z1 <- c(-0.28, 0.09, 0.05, 0.24)
cols <- c("#1B9E77", "#D95F02", "#7570B3", "#E7298A")
# 3D Arrows:
arrows3D(x0, y0, z0, x1, y1, z1, colvar = x1^2, col = cols,
lwd = 2, d = 3, clab = c("Quality", "score"),
main = "Arrows 3D", bty ="g", ticktype = "detailed")
# Add starting point of arrow
points3D(x0, y0, z0, add = TRUE, col="darkred",
colkey = FALSE, pch = 19, cex = 1)
# Add labels to the arrows
text3D(x1, y1, z1, c("Sepal.L", "Sepal.W", "Petal.L", "Petal.W"),
colvar = x1^2, col = cols, add=TRUE, colkey = FALSE)
# 2D arrows:
arrows2D(x0, y0, x1, y1, colvar = x1^2, col = cols,
lwd = 2, clab = c("Quality", "score"),
bty ="n", xlim = c(-1, 1), ylim = c(-1, 1))
# Add vertical and horizontal lines at c(0,0)
abline(h =0, v = 0, lty = 2)
# Add starting point of arrow
points2D(x0, y0, add = TRUE, col="darkred",
colkey = FALSE, pch = 19, cex = 1)
# Add labels to the arrows
text2D(x1, y1, c("Sepal.L", "Sepal.W", "Petal.L", "Petal.W"),
colvar = x1^2, col = cols, add=TRUE, colkey = FALSE)
# Note that, segments3D() and segments2D() are very similar to arrows3D() and arrows2D() and you can
play with them also.
# 3D rectangle: the R code below creates a rectangle with a transparent fill color (alpha = 0.5)
rect3D(x0 = 0, y0 = 0.5, z0 = 0, x1 = 1, z1 = 5,
ylim = c(0, 1), bty = "g", facets = TRUE,
border = "red", col ="#7570B3", alpha=0.5,
lwd = 2, phi = 20)
# 2D rectangle
rect2D(x0 = runif(3), y0 = runif(3),
x1 = runif(3), y1 = runif(3), colvar = 1:3,
alpha = 0.4, lwd = 2, main = "rect2D")
# Interactive plot
# To draw an interactive 3D plot the package plot3Drgl can be used.
#
# The package plot3Drgl allows to plot the graph generated with plot3D in openGL, as made available by
package rgl.
#
# The simplest way is to do as follow:
#
# Create base R-graphics using plot3D package
# Then use the function plotrgl() to draw the same figure in rgl
# Create his3D using plot3D
hist3D_fancy(iris$Sepal.Length, iris$Petal.Width, colvar=as.numeric(iris$Species))
# Make the rgl version
library("plot3Drgl")
plotrgl()
# Note that, after creating the rgl plot you can use the functions below:
#
# croprgl(xlim, ylim, zlim, …) to modify the ranges
# cutrgl(…) to zoom in on a selected region of the plot. The current plot will be overwritten
# uncutrgl(…) and uncroprgl(…) restore the original plot.
#
# …: any arguments for par3d, open3d or material3d in rgl package.