How To Create 3D Plots in R
How To Create 3D Plots in R
(With Examples)
#create z-values
z = outer(x, y, z_values)
#create 3D plot
persp(x, y, z)
Example 2: Custom 3D Plot
The following code shows how to customize the axis labels,
title, color, and shade of the plot:
#define x and y
x <- -10:10
y <- -10:10
#create z-values
z = outer(x, y, z_values)
#create 3D plot
persp(x, y, z, xlab='X Variable', ylab='Y Variable', zlab='Z Variable',
main='3D Plot', col='pink', shade=.4)
Example 3: Rotate the 3D Plot
The following code shows how to rotate the 3D plot to make it
easier to view, using the theta and phi arguments:
#define x and y
x <- -10:10
y <- -10:10
#create z-values
z = outer(x, y, z_values)
#create 3D plot
persp(x, y, z, xlab='X Variable', ylab='Y Variable', zlab='Z Variable',
main='3D Plot', col='pink', shade=.4, theta = 30, phi = 15)
Example 4: Add Tick Marks to the 3D
Plot
The following code shows how to use
the ticktype argument to add tick marks with labels to each
axis:
#define x and y
x <- -10:10
y <- -10:10
#create z-values
z = outer(x, y, z_values)
#create 3D plot
persp(x, y, z, xlab='X Variable', ylab='Y Variable', zlab='Z Variable',
main='3D Plot', col='pink', shade=.4, theta = 30, phi = 15,
ticktype='detailed')