Tuesday, August 26, 2025
Thursday, November 14, 2024
Day 4: 30-days to learn rgl, plotly, and gganimate - Installing rgl and Creating Basic 3D Plots
Step 1: Install and Load rgl
1. Install the rgl package (if you haven’t already):
install.packages
("rgl")
2. Load the rgl library:
library
(rgl
)
Step 2: Create a Basic 3D Scatter Plot using plot3d
1. Generate sample data for plotting:
set.seed
(123)
x
<- rnorm
(100)
y
<- rnorm
(100)
z
<- rnorm
(100)
2. Create a 3D scatter plot using plot3d
:
plot3d
(x
, y
, z
, col
=
"blue", size
=
5, type
=
"s")
col
: Set the color of the points.size
: Control the point size.type
: "s" represents spheres. You can also try other types, like "p" for points.
The plot should now open in an interactive window,
allowing you to view your data in 3D.
Step 3: Create a 3D Scatter Plot using scatter3d
1. Install and load the car
package to
access the scatter3d
function:
install.packages
("car")
library
(car
)
2. Use scatter3d
for a quick 3D scatter plot:
scatter3d
(x
, y
, z
, surface
=
FALSE, fit
=
"linear")
surface
: Set toFALSE
if you don't want a regression surface (3D plane).fit
: Set to "linear" for a linear fit (you can also use other fits if you like).
Step 4: Exploring Basic Camera Rotations and Zoom
Functions
rgl
provides several
functions to interactively control the camera's view, allowing you to rotate,
zoom, and pan.
1. Rotate the Camera:
- The
rgl.viewpoint()
function allows you to set the camera angle. Try the following command:
rgl.viewpoint
(theta
=
45, phi
=
30)
theta
: Controls rotation around the z-axis.phi
: Controls the vertical rotation.
2. Zoom In and Out:
- Adjust
the zoom level using the
zoom
parameter inrgl.viewpoint
:
rgl.viewpoint
(theta
=
45, phi
=
30, zoom
=
0.7)
- A
zoom value less than 1 will zoom out, while a value greater than 1 will
zoom in.
3. Play Around with Different Viewpoints:
- Experiment
with
theta
,phi
, andzoom
to see how they change your perspective. Here are a few examples:
# Top-down view
rgl.viewpoint
(theta
=
90, phi
=
90, zoom
=
0.5)
# Side view
rgl.viewpoint
(theta
=
0, phi
=
0, zoom
=
0.8)
# Rotate incrementally
for
(angle
in seq
(0,
360, by
=
10))
{
rgl.viewpoint
(theta
= angle
, phi
=
30)
Sys.sleep
(0.1)
}
Step 5: Practice and Experiment
Explore rgl
further by
experimenting with different colors, sizes, and types of points in plot3d
,
or by adjusting the viewpoint using theta
and phi
in rgl.viewpoint
.
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
, andz = ~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
andy = ~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, includingxaxis
,yaxis
, andzaxis
.title
: Sets custom titles for each axis.backgroundcolor
andgridcolor
: 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
, andPortland
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.
-
Q: What do you know about ergot alkaloids? Ans: These include alkaloids which we get from the ergot fungus Claviceps purpurea or derived ...
-
It is the loss of water from a crystal. It means “to flower out” in French. It is the spontaneous loss of water (or solvent) from a hyd...
-
(For detailed study of Pharmaceutical Incompatibility Click here) Multiple Choice Questions (MCQs) from Pharmaceutical Incompatibility in ...