Step 1: Create a Basic 3D Plot
We'll begin by creating a simple 3D scatter plot to serve as our canvas for
customization.
# Load the rgl packagelibrary(rgl) # Sample data for 3D scatter plotset.seed(123)x <- rnorm(10)y <- rnorm(10)z <- rnorm(10) # Basic 3D scatter plotplot3d(x, y, z, type = "s", col = "blue", size = 2)
This creates a simple 3D scatter plot with blue points.
Step 2: Customize Point Colors, Sizes, and Shapes
Adjusting colors, point sizes, and shapes can make the plot more visually informative.
# Customizing colors, sizes, and shapespoint_colors <- rainbow(length(x)) # Assign each point a different colorplot3d(x, y, z, col = point_colors, size = 5, type = "s") # Increased size
In this example:
- Each point
gets a unique color.
sizecontrols the size of the points (default is smaller; try 5 for larger points).type = "s"sets the points as spheres.
Step 3: Add Lighting Effects
Lighting can add depth and realism to your plot.
# Adding lighting effectsopen3d() # Open a new rgl windowplot3d(x, y, z, col = point_colors, size = 5, type = "s") # Adjust lighting - you can experiment with these settingsrgl.light(ambient = "#FFFFFF", specular = "#BBBBBB", diffuse = "#888888", x = 1, y = 1, z = 1)
Lighting parameters:
ambient: Light that scatters evenly.specular: Shiny highlights on objects.diffuse: Light scattered by the surface.
Experiment with these values to get the desired effect.
Step 4: Customize Camera Angles and Views
You can set the viewing angle to emphasize specific points or sections.
# Adjust camera anglergl.viewpoint(theta = 30, phi = 30, fov = 60)
Parameters:
thetaandphi: Control the angles from which the plot is viewed.fov: Field of view; increasing it creates a wide-angle view, while lowering it zooms in.
Step 5: Add Labels and Text to 3D Points
Labels help make your data points identifiable, enhancing interpretability.
# Adding labels to each pointtext3d(x, y, z, texts = paste("Pt", 1:10), col = "black", cex = 0.8)
Explanation:
text3d: Adds text at each point’s location.texts: Vector of labels for each point.colandcex: Customize text color and size.
Step 6: Experiment with Size and Color Gradients
You can set sizes and colors based on data values to make plots more
meaningful.
# Adjust point sizes and color gradient based on z-valuessize_values <- rescale(z, to = c(3, 10)) # Scale sizes between 3 and 10color_gradient <- heat.colors(length(z)) plot3d(x, y, z, col = color_gradient, size = size_values, type = "s")
Step 7: Adding Legends for Better Interpretation
(Optional)
Legends help viewers interpret what different colors or sizes represent.
# Add a simple legend to the plotlegend3d("topright", legend = c("Low", "Medium", "High"), pch = 16, col = c("blue", "green", "red"), cex = 1.2)
Step 8: Save the Interactive 3D Plot (Optional)
To save your 3D plot as an HTML file for interactive viewing:
# Save as HTML file for interactive userglwidget() %>% htmlwidgets::saveWidget("3Dplot.html")
This file can then be shared or viewed interactively in a browser.
Summary
Today, you customized an rgl
3D plot by:
- Adjusting
lighting, colors, sizes, and shapes.
- Adding
text labels to 3D points for better interpretation.
- Experimenting
with camera angles and views.
