Upload
others
View
7
Download
0
Embed Size (px)
Citation preview
Data Science: Data Visualization Boot Camp3D
R’s rgl package
Chuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhDChuck Cartledge, PhD
26 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 202026 January 2020
1/28
2/28
Type Sample data Hands on Q & A Conclusion References Files
Table of contents (1 of 1)
1 TypeUsesGeneral considerations
2 Sample data
3 Hands on
4 Q & A
5 Conclusion6 References7 Files
3/28
Type Sample data Hands on Q & A Conclusion References Files
A definition
“The phrase three-dimensional (some-times abbreviated 3-D) graph is used in twosignificantly different ways, which sometimescauses confusion. In one case the phrase isused to mean that a graph has the appear-ance of having depth which makes it lookthree-dimensional. This frequently is donefor cosmetic purposes. In the other case,the phrase three-dimensional graph refers toa graph that has three axes. This may bedone for cosmetic purposes or to display thedata in a way that simplifies an overall anal-ysis or provides insights not possible in otherformats.”
R. L. Harris [2]
4/28
Type Sample data Hands on Q & A Conclusion References Files
Motor Trend R supplied data set (1 of 2)
Included in the R package datasets.
“The data was extracted from the 1974 Motor TrendUS magazine, and comprises fuel consumption and 10 as-pects of automobile design and performance for 32 auto-mobiles (1973-74 models).”
R Core Team [4]
?mtcars
head(mtcars)
Resulting in:
5/28
Type Sample data Hands on Q & A Conclusion References Files
Motor Trend R supplied data set (2 of 2)
mpg cyl disp hp drat wt qsec vs am gear carb col
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 red
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 red
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 green
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 red
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 blue
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 red
6/28
Type Sample data Hands on Q & A Conclusion References Files
Volcano R supplied data set (1 of 2)
Included in the R package datasets.
“Maunga Whau (Mt Eden) is one of about 50 volcanosin the Auckland volcanic field. This data set gives topo-graphic information for Maunga Whau on a 10m by 10mgrid.”
R Core Team [4]
?volcano
head(volcano)
Resulting in:
7/28
Type Sample data Hands on Q & A Conclusion References Files
Volcano R supplied data set (2 of 2)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] 100 100 101 101 101 101 101 100 100 100 101 101 102 102
[2,] 101 101 102 102 102 102 102 101 101 101 102 102 103 103
[3,] 102 102 103 103 103 103 103 102 102 102 103 103 104 104
[4,] 103 103 104 104 104 104 104 103 103 103 103 104 104 104
[5,] 104 104 105 105 105 105 105 104 104 103 104 104 105 105
[6,] 105 105 105 106 106 106 106 105 105 104 104 105 105 106
(Output truncated.)
8/28
Type Sample data Hands on Q & A Conclusion References Files
Norfolk Island elevation data (1 of 2)
Included in the R package datasets.
“. . . lets look at a digital elevation model of NorfolkIsland. This model was built as part of a Master’s studentproject to study rainfall on the island.”
R. Ihaka [3]
source(‘‘http://www.stat.auckland.ac.nz/~haka/
120/Data/ni-smoothed.R’’)
head(smoothed)
Resulting in:
9/28
Type Sample data Hands on Q & A Conclusion References Files
Norfolk Island elevation data (2 of 2)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]
[1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[2,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[4,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
[1,] 0 0 0 0 0 0 0 0 0 0 0 0
[2,] 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 0 0 0 0 0 0 0 0 0 0 0 0
[4,] 0 0 0 0 0 0 0 0 0 0 0 0
[5,] 0 0 0 0 0 0 0 0 0 0 0 0
[6,] 0 0 0 0 0 0 0 0 0 0 0 0
Output truncated.
10/28
Type Sample data Hands on Q & A Conclusion References Files
The first codes. (1 of 6)
11/28
Type Sample data Hands on Q & A Conclusion References Files
The first codes. (2 of 6)
rm(list=ls())
library(rgl)
library(ggplot2)
library(knitr)
library(rglwidget)
library(scales)
library(modeest)
library(clusterSim)
plot3d(mtcars$wt, mtcars$disp, mtcars$mpg,
type="s",
size=1,
lit=TRUE,
main = "Car Weight Vs Engine Displacement Vs Mileage",
sub="",
xlab = "Weight (1,000 lbs)",
ylab="Engine disp (cu in.)",
12/28
Type Sample data Hands on Q & A Conclusion References Files
The first codes. (3 of 6)
zlab = "Mileage"
)
plot3d(mtcars$wt, mtcars$disp, mtcars$mpg,
type="p",
size=5,
lit=FALSE,
box=FALSE,
col = c("red","blue","green"),
expand = 1,
main = "Car Weight Vs Engine Displacement Vs Mileage",
sub="",
xlab = "Weight (1,000 lbs)",
ylab="Engine disp (cu in.)",
zlab = "Mileage"
)
mtcars$col
13/28
Type Sample data Hands on Q & A Conclusion References Files
The first codes. (4 of 6)
mtcars$col[which(mtcars$cyl == 4)]
14/28
Type Sample data Hands on Q & A Conclusion References Files
The first codes. (5 of 6)
rgl.close()
rgl.open()
rgl.init()
rgl.bg(color="white")
rgl.spheres(
x=mtcars$wt,
y=mtcars$disp,
z=mtcars$mpg,
r=10,
color=mtcars$col
)
aspect3d(1,1,1)
axes3d(edges="bbox")
## axes3d(edges=c(’x’,’y’,’z’))
title3d(
main = "Car Weight Vs Engine Displacement Vs Mileage",
sub="",
xlab = "Weight (1,000 lbs)",
15/28
Type Sample data Hands on Q & A Conclusion References Files
The first codes. (6 of 6)
ylab="Engine disp (cu in.)",
zlab = "Mileage",
col="blue"
)
16/28
Type Sample data Hands on Q & A Conclusion References Files
The second codes. (1 of 4)
Idea taken from [3].
17/28
Type Sample data Hands on Q & A Conclusion References Files
The second codes. (2 of 4)
rm(list=ls())
library(grDevices)
library(rgl)
data(volcano)
z
18/28
Type Sample data Hands on Q & A Conclusion References Files
The second codes. (3 of 4)
open3d()
surface3d(x, y, z, color = col, back = "lines")
source("https://github.com/mamonu/Ropengl/raw/master/ni-smoothed.r")
heights = smoothed
rgl.clear()
rgl.clear("lights")
rgl.light(theta=0,phi=45)
ncolors = 100
colorlut = terrain.colors(ncolors)
colorlut[1] = "steelblue"
xg = seq(xlim[1],xlim[2],length=n)
yg = seq(ylim[1],ylim[2],length=n)
ymax = max(heights)
ymin = min(heights)
19/28
Type Sample data Hands on Q & A Conclusion References Files
The second codes. (4 of 4)
col = colorlut[1 + (ncolors - 1) * (heights - ymin)/(ymax - ymin)]
rgl.surface(yg, xg, heights, color=col,
smooth=TRUE, specular="#000000")
20/28
Type Sample data Hands on Q & A Conclusion References Files
The third codes. (1 of 4)
Ideas from: “subscene3d: Create, select or modify a subscene”[1].
21/28
Type Sample data Hands on Q & A Conclusion References Files
The third codes. (2 of 4)
rm(list=ls())
library(rgl)
lat
22/28
Type Sample data Hands on Q & A Conclusion References Files
The third codes. (3 of 4)
texture=system.file("textures/world.png",
package="rgl"),
specular="black", axes=FALSE, box=FALSE,
xlab="", ylab="", zlab="",
normal_x=x, normal_y=y, normal_z=z),
spheres3d(matrix(0, 9, 3), col = cols, radius = rs))
root
23/28
Type Sample data Hands on Q & A Conclusion References Files
The third codes. (4 of 4)
viewport = "inherit", copyShapes = TRUE, parent = root)
clipplanes3d(0,0,-1,0)
delFromSubscene3d(obj, subscene = root)
24/28
Type Sample data Hands on Q & A Conclusion References Files
Hands-on exercises
1 In the first set of slides, your supervisor wants just onechange.
1 He wants to see which of the cars has a “Straight” engine, andwhich have a “V” engine.
2 In the second set of slides:1 Provide a way to “cycle” through the various color pallets to
see which looks best.
3 In the third set of slides:1 Change the world to an orange.
25/28
Type Sample data Hands on Q & A Conclusion References Files
Q & A time.
Q: How many Zen masters doesit take to screw in a light bulb?A: None. The Universe spins thebulb, and the Zen master staysout of the way.
26/28
Type Sample data Hands on Q & A Conclusion References Files
What have we covered?
There are a multitude of differentaspects to 3D plots.
They are very good for displayingdata with more than one or twoattributes.They can be very difficult toexplain because of their higherdimesionality.The interaction between differentplotting commands can bedifficult to control.Interactive plots can be veryuseful.
There are times when they are thebest.
Next: Looking at graphs and plots with different eyes.
27/28
Type Sample data Hands on Q & A Conclusion References Files
References (1 of 1)
[1] Daniel Adler, Duncan Murdoch, and others, subscene3d:Create, select or modify a subscene,https://rdrr.io/rforge/rgl/man/subscene3d.html,2018, R package version 0.99.16.
[2] Robert L. Harris,Information Graphics: A Comprehensive Illustrated Reference,Oxford University Press, 2000.
[3] Ross Ihaka, Three-D Visualisation and Animation,https://www.stat.auckland.ac.nz/~ihaka/120/
Tutorials/lab09.html, 2003.
[4] R Core Team,R: A Language and Environment for Statistical Computing, RFoundation for Statistical Computing, Vienna, Austria, 2018.
https://rdrr.io/rforge/rgl/man/subscene3d.htmlhttps://www.stat.auckland.ac.nz/~ihaka/120/Tutorials/lab09.htmlhttps://www.stat.auckland.ac.nz/~ihaka/120/Tutorials/lab09.html
28/28
Type Sample data Hands on Q & A Conclusion References Files
Files of interest
1 Code snippet to createimages in this presentation
2 Elevation data from
https:
//github.com/mamonu/
Ropengl/raw/master/
ni-smoothed.r
## First codesrm(list=ls())
library(rgl)library(ggplot2)library(knitr)library(rglwidget)library(scales)library(modeest)library(clusterSim)
plot3d(mtcars$wt, mtcars$disp, mtcars$mpg, type="s", size=1, lit=TRUE, main = "Car Weight Vs Engine Displacement Vs Mileage", sub="", xlab = "Weight (1,000 lbs)", ylab="Engine disp (cu in.)", zlab = "Mileage" )
plot3d(mtcars$wt, mtcars$disp, mtcars$mpg, type="p", size=5, lit=FALSE, box=FALSE, col = c("red","blue","green"), expand = 1, main = "Car Weight Vs Engine Displacement Vs Mileage", sub="", xlab = "Weight (1,000 lbs)", ylab="Engine disp (cu in.)", zlab = "Mileage" )
mtcars$col