R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Creating 3D plots can be a powerful way to visualize multivariate data. In R, there are several packages available for creating 3D visualizations. In this tutorial, we'll cover how to create 3D plots using the rgl
and scatterplot3d
packages.
scatterplot3d
:The scatterplot3d
package provides a method to create 3D scatter plots.
Installation:
install.packages("scatterplot3d")
Basic 3D Scatter Plot:
library(scatterplot3d) # Sample data set.seed(123) data <- data.frame(X = rnorm(100), Y = rnorm(100), Z = rnorm(100)) # Create 3D scatter plot scatterplot3d(data$X, data$Y, data$Z, main="3D Scatter Plot", xlab="X-axis", ylab="Y-axis", zlab="Z-axis")
rgl
:The rgl
package offers more interactive 3D visualizations. You can rotate and zoom the plot.
Installation:
install.packages("rgl")
Basic 3D Scatter Plot:
library(rgl) # Sample data set.seed(123) data <- data.frame(X = rnorm(100), Y = rnorm(100), Z = rnorm(100)) # Create 3D scatter plot plot3d(data$X, data$Y, data$Z, type="s", col="red", size=0.5, axes=TRUE, xlab="X-axis", ylab="Y-axis", zlab="Z-axis")
After plotting, you can interactively rotate the plot using your mouse.
You can also create 3D surface plots using rgl
.
# Create a grid of values x <- seq(-10, 10, length.out=30) y <- x z <- outer(x, y, function(x, y) { r <- sqrt(x^2 + y^2); sin(r)/r }) # Create 3D surface plot persp3d(x, y, z, col="lightblue", xlab="X-axis", ylab="Y-axis", zlab="Z-axis")
The rgl
package opens up a new window for the interactive 3D plot. To close this window programmatically, you can use:
rgl.close()
3D plots are useful for visualizing multivariate relationships and can provide insights that might be missed in 2D projections. However, it's important to note that 3D visualizations can sometimes be challenging to interpret, especially in static formats. Whenever using 3D plots, ensure that the added dimension genuinely adds clarity to the data presentation.
Scatterplot3d in R:
# Install and load the scatterplot3d package # install.packages("scatterplot3d") library(scatterplot3d) # Create 3D scatter plot x <- c(1, 2, 3, 4, 5) y <- c(2, 3, 4, 5, 6) z <- c(3, 4, 5, 6, 7) scatterplot3d(x, y, z, main = "3D Scatterplot")
Surface plot in R:
# Create data for surface plot x <- seq(-5, 5, length.out = 100) y <- seq(-5, 5, length.out = 100) z <- outer(x, y, function(x, y) sin(sqrt(x^2 + y^2))) # Create surface plot persp(x, y, z, main = "Surface Plot")
3D scatter plot with ggplot2 in R:
# Install and load the ggplot2 package # install.packages("ggplot2") library(ggplot2) # Create 3D scatter plot with ggplot2 df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(2, 3, 4, 5, 6), z = c(3, 4, 5, 6, 7)) ggplot(df, aes(x, y, z = z)) + geom_point_3d() + labs(title = "3D Scatter Plot with ggplot2")
R wireframe plot example:
# Create data for wireframe plot x <- seq(-5, 5, length.out = 100) y <- seq(-5, 5, length.out = 100) z <- outer(x, y, function(x, y) sin(sqrt(x^2 + y^2))) # Create wireframe plot wireframe(z ~ x * y, data = as.data.frame(expand.grid(x, y)), main = "Wireframe Plot")
Interactive 3D plots in R:
For interactive 3D plots, you can use packages like plotly
or rgl
.
Example using plotly
:
# Install and load the plotly package # install.packages("plotly") library(plotly) # Create 3D scatter plot with plotly df <- data.frame(x = c(1, 2, 3, 4, 5), y = c(2, 3, 4, 5, 6), z = c(3, 4, 5, 6, 7)) plot_ly(df, x = ~x, y = ~y, z = ~z, type = "scatter3d", mode = "markers") %>% layout(scene = list(title = "Interactive 3D Scatter Plot"))
R lattice 3D plot:
# Install and load the lattice package # install.packages("lattice") library(lattice) # Create 3D lattice plot x <- seq(-5, 5, length.out = 100) y <- seq(-5, 5, length.out = 100) z <- outer(x, y, function(x, y) sin(sqrt(x^2 + y^2))) # Create lattice plot wireframe(z ~ x * y, data = as.data.frame(expand.grid(x, y)), main = "Lattice 3D Plot")
3D density plot in R:
# Install and load the plot3D package # install.packages("plot3D") library(plot3D) # Create 3D density plot x <- rnorm(100) y <- rnorm(100) z <- dnorm(x) * dnorm(y) # Create 3D density plot persp3D(x, y, z, colvar = z, col = "blue", main = "3D Density Plot")