Wednesday, November 13, 2024

Day 3: 30-days to learn rgl, plotly, and gganimate - Dive into 3D plotting with plotly, exploring 3D scatter and surface plots, and customizing axes and color scales




Step 1: Set Up for 3D Plotting

1.     Load plotly: Make sure plotly is loaded in your R environment.

library(plotly)

2.     Create Sample Data: Let’s create sample data for our 3D scatter plot and surface plot.

# Data for 3D Scatter Plot
x_scatter <- rnorm(50)
y_scatter <- rnorm(50)
z_scatter <- rnorm(50)
 
# Data for 3D Surface Plot
x_surface <- seq(-10, 10, length.out = 50)
y_surface <- seq(-10, 10, length.out = 50)
z_surface <- outer(x_surface, y_surface, function(x, y) sin(sqrt(x^2 + y^2)))

Step 2: Create a 3D Scatter Plot

1.     Basic 3D Scatter Plot: Use plot_ly() with type = 'scatter3d' to create a basic 3D scatter plot.

scatter3d_plot <- plot_ly(x = ~x_scatter, y = ~y_scatter, z = ~z_scatter, 
                          type = 'scatter3d', mode = 'markers')
scatter3d_plot
    • Explanation:
      • x = ~x_scatter, y = ~y_scatter, and z = ~z_scatter: Set the x, y, and z coordinates.
      • type = 'scatter3d': Specifies a 3D scatter plot.
      • mode = 'markers': Displays only markers (points) in 3D space.

2.     Customize Marker Colors and Sizes:

scatter3d_plot <- plot_ly(x = ~x_scatter, y = ~y_scatter, z = ~z_scatter, 
                          type = 'scatter3d', mode = 'markers',
                          marker = list(size = 5, color = ~z_scatter, colorscale = 'Viridis'))
scatter3d_plot
    • Explanation:
      • size = 5: Adjusts the size of the markers.
      • color = ~z_scatter: Colors markers based on their z values.
      • colorscale = 'Viridis': Applies a color scale to the z values.

Step 3: Create a 3D Surface Plot

1.     Basic 3D Surface Plot: Use plot_ly() with type = 'surface' to create a basic 3D surface plot.

surface_plot <- plot_ly(z = ~z_surface, x = ~x_surface, y = ~y_surface, type = 'surface')
surface_plot
    • Explanation:
      • z = ~z_surface: Sets the height values of the surface.
      • x = ~x_surface and y = ~y_surface: Sets the x and y coordinates for the surface plot.
      • type = 'surface': Specifies a 3D surface plot.

2.     Customize the Surface Plot with a Color Scale:

surface_plot <- plot_ly(z = ~z_surface, x = ~x_surface, y = ~y_surface, 
                        type = 'surface', colorscale = 'Viridis')
surface_plot
    • Explanation:
      • colorscale = 'Viridis': Adds a Viridis color scale, enhancing the visualization of depth.

Step 4: Customize Axes

You can adjust the axis properties to improve the plot’s readability and style.

1.     Customize 3D Scatter Plot Axes:

scatter3d_plot <- scatter3d_plot %>%
  layout(scene = list(xaxis = list(title = "X Axis", backgroundcolor = "lightgrey", gridcolor = "white"),
                      yaxis = list(title = "Y Axis", backgroundcolor = "lightgrey", gridcolor = "white"),
                      zaxis = list(title = "Z Axis", backgroundcolor = "lightgrey", gridcolor = "white")))
scatter3d_plot
    • Explanation:
      • scene: Defines 3D plot layout options, including xaxis, yaxis, and zaxis.
      • title: Sets custom titles for each axis.
      • backgroundcolor and gridcolor: Adjust axis background and grid colors.

2.     Customize 3D Surface Plot Axes:

surface_plot <- surface_plot %>%
  layout(scene = list(xaxis = list(title = "X Axis", color = "blue"),
                      yaxis = list(title = "Y Axis", color = "green"),
                      zaxis = list(title = "Z Axis", color = "red")))
surface_plot

Step 5: Experiment with Different Color Scales

plotly provides several color scales. Try using different ones to see their effects on your plot.

1.     Apply Different Color Scales to the 3D Scatter Plot:

scatter3d_plot <- plot_ly(x = ~x_scatter, y = ~y_scatter, z = ~z_scatter, 
                          type = 'scatter3d', mode = 'markers',
                          marker = list(size = 5, color = ~z_scatter, colorscale = 'Cividis'))
scatter3d_plot

2.     Apply Different Color Scales to the 3D Surface Plot:

surface_plot <- plot_ly(z = ~z_surface, x = ~x_surface, y = ~y_surface, 
                        type = 'surface', colorscale = 'Electric')
surface_plot
    • Other Color Scales:
      • Try color scales like Jet, Bluered, RdBu, YlGnBu, Greens, and Portland to see different effects.

Step 6: Combine All Customizations

Let’s bring everything together to create a polished 3D scatter and 3D surface plot.

Final 3D Scatter Plot with Customizations

final_scatter3d <- plot_ly(x = ~x_scatter, y = ~y_scatter, z = ~z_scatter, 
                           type = 'scatter3d', mode = 'markers',
                           marker = list(size = 5, color = ~z_scatter, colorscale = 'Viridis')) %>%
  layout(scene = list(xaxis = list(title = "X Axis", backgroundcolor = "lightgrey", gridcolor = "white"),
                      yaxis = list(title = "Y Axis", backgroundcolor = "lightgrey", gridcolor = "white"),
                      zaxis = list(title = "Z Axis", backgroundcolor = "lightgrey", gridcolor = "white")))
final_scatter3d

Final 3D Surface Plot with Customizations

final_surface_plot <- plot_ly(z = ~z_surface, x = ~x_surface, y = ~y_surface, 
                              type = 'surface', colorscale = 'Electric') %>%
  layout(scene = list(xaxis = list(title = "X Axis", color = "blue"),
                      yaxis = list(title = "Y Axis", color = "green"),
                      zaxis = list(title = "Z Axis", color = "red")))
final_surface_plot

Summary

Today, you:

  • Learned to create 3D scatter and surface plots using plotly.
  • Customized marker colors and sizes for 3D scatter plots.
  • Applied color scales to enhance visual appeal.
  • Customized axes in 3D space, including titles, colors, and backgrounds.

Next, you’ll build on this knowledge by diving into more complex plot customizations and interactive features to make the plots even more informative.

No comments: