3D Plot in R Programming-Quick Guide
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
3D Plot in R, one of the quickest ways to create a 3D plot is to use the persp() function.
3D plot the data points on three axes to highlight the link between three factors.
Let’s see the syntax.
persp(x, y, z)
Approach 1: Basic 3D Plot in R
Let’s create a basic 3D plot,
#define x and y
x <- -20:20 y <- -20:20
we create a z value based on the function of x and y
QQ-plots in R: Quantile-Quantile Plots-Quick Start Guide »
z_val <- function(x, y) { sqrt(x ^ 2 + y ^ 2)}
#create z-values
z<-outer(x, y, z_val)
#create a 3D plot
persp(x, y, z)
Approach 2: Customisation
Let’s change the axis labels, title, color, and shade of the plot
aggregate Function in R- A powerful tool for data frames »
#create a 3D plot
persp(x, y, z, xlab='X Variable', ylab='Y Variable', zlab='Z Variable', main='3D Plot', col=”orange”, shade=.4)
Approach 3: Rotate the 3D Plot
Sometimes need to rotate the 3D plot to make it better understanding, then make use of the theta and phi arguments:
Compare data frames in R-Quick Guide »
#create a 3D plot
persp(x, y, z, xlab='X Variable', ylab='Y Variable', zlab='Z Variable', main='3D Plot', col='red', shade=.4, theta = 30, phi = 15)
Approach 4: Add Tick Marks to the 3D Plot
The following code shows how to use the tick type argument to add tick marks with labels to each axis:
How to find z score in R-Easy Calculation-Quick Guide »
#create a 3D plot
persp(x, y, z, xlab='X Variable', ylab='Y Variable', zlab='Z Variable', main='3D Plot', col='blue', shade=.4, theta = 30, phi = 15, ticktype='detailed')
How to Calculate Phi Coefficient in R » Association »
The post 3D Plot in R Programming-Quick Guide appeared first on finnstats.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.