Graphics in RSTAT 133
Gaston Sanchez
Department of Statistics, UC–Berkeley
gastonsanchez.com
github.com/gastonstat/stat133
Course web: gastonsanchez.com/stat133
R Graphics
2
Understanding Graphics in R
2 main graphics systems
"graphics" & "grid"
3
Basics of Graphics in R
Graphics Systems
I "graphics" and "grid" are the two main graphicssystems in R
I "graphics" is the traditional system, also referred to asbase graphics
I "grid" prodives low-level functions for programmingplotting functions
4
Basics of Graphics in R
Graphics Engine
I Underneath "graphics" and "grid" there is the package"grDevices"
I "grDevices" is the graphics engine in R
I It provides the graphics devices and support for colors andfonts
5
grid
graphics
grDevices
maps diagram plotrix
ggplot2
lattice
tikzDevice
JavaGD
Cairo
6
Basics of Graphics in R
Package "graphics"
The package "graphics" is the traditional system; it providesfunctions for complete plots, as well as low-level facilities.
Many other graphics packages are built on top of graphics like"maps", "diagram", "pixmap", and many more.
7
Understanding Graphics in R
Package "grid"
The "grid" package does not provide functions for drawingcomplete plots.
"grid" is not used directly to produce statistical plots. Instead,it is used to build other graphics packages like "lattice" or"ggplot2".
8
In this course
I In this course we’ll focus on the packages "graphics" and"ggplot2"
I "graphics" is the traditional plotting system in R, andmany functions and packages are built on top of it.
I "ggplot2" excels at providing graphics for visualizingmultivariate data sets —in data.frame format—, whiletaking care of many issues for superior visual displays.
9
R Graphics by Paul Murrell
10
Some Resources
I R Graphics by Paul Murrellbook and webpage
I R Graphics Cookbook by Winston Changhttp://www.cookbook-r.com/Graphs/
I ggplot2: Elegant Graphics for Data Analysis byHadley Wickham
I R Graphs Cookbook by Hrishi Mittal
I Graphics for Statistics and Data Analysis with R byKevin Keen
11
Traditional (Base) Graphics
12
Base Graphics in R
Types of graphics functionsGraphics functions can be divided into two main types:
I high-level functions produce complete plots, e.g.barplot(), boxplot(), dotchart()
I low-level functions add further output to an existing plot,e.g. text(), points(), legend()
13
The plot() function
I plot() is the most important high-level function intraditional graphics
I The first argument to plot() provides the data to plot
I The provided data can take different forms: e.g. vectors,factors, matrices, data frames.
I To be more precise, plot() is a generic function
I You can create your own plot() method function
14
Basic Plots with plot()
In its basic form, we can use plot() to make graphics of:
I one single variable
I two variables
I multiple variables
15
Plots of One Variable
16
High-level graphics of a single variable
Function Data Descriptionplot() numeric scatterplotplot() factor barplotplot() 1-D table barplot
numeric can be either a vector or a 1-D array (e.g. row orcolumn from a matrix)
17
One variable objects
Vector / Factor
row (data.frame)
row (matrix)
1-D table
column (data.frame)
column (matrix)
18
plot() of one variable
# plot numeric vector
num_vec <- (c(1:10))^2
plot(num_vec)
# plot factor
set.seed(4)
abc <- factor(sample(c('A', 'B', 'C'), 20, replace = TRUE))
plot(abc)
# plot 1D-table
abc_table <- table(abc)
plot(abc_table)
19
plot() of one variable
●●
●
●
●
●
●
●
●
●
2 4 6 8 10
02
04
06
08
01
00
Index
nu
m_
ve
c
A B C
02
46
8
02
46
8
abc
ab
c_
table
A B C
20
More high-level graphics of a single variable
Function Data Descriptionbarplot() numeric barplotpie() numeric pie chartdotchart() numeric dotplot
boxplot() numeric boxplothist() numeric histogramstripchart() numeric 1-D scatterplotstem() numeric stem-and-leaf plot
21
Plots of one variable
# barplot numeric vector
barplot(num_vec)
# pie chart
pie(1:3)
# dot plot
dotchart(num_vec)
22
Plots of one variable
02
04
06
08
01
00
1
2
3●
●
●
●
●
●
●
●
●
●
0 20 40 60 80 100
23
Plots of one variable
# barplot numeric vector
boxplot(num_vec)
# pie chart
hist(num_vec)
# dot plot
stripchart(num_vec)
# stem-and-leaf
stem(num_vec)
24
boxplot()
# boxplot
boxplot(iris$Sepal.Length)
4.5
5.0
5.5
6.0
6.5
7.0
7.5
8.0
25
hist()
# histogram
hist(iris$Sepal.Length)
Histogram of iris$Sepal.Length
iris$Sepal.Length
Fre
quen
cy
4 5 6 7 8
05
1015
2025
30
26
Test your knowledge
What option does not apply to histograms:
A) adjacent bars (no gaps)
B) area of bars indicate proportions
C) bins of equal length
D) bars can be reordered
27
stripchart()
# strip-chart (1-D scatter plot)
# (for small sample sizes)
stripchart(num_vec)
0 20 40 60 80 100
28
stem()
# stem-and-leaf plot
# (for small sample sizes)
stem(num_vec)
##
## The decimal point is 1 digit(s) to the right of the |
##
## 0 | 1496
## 2 | 56
## 4 | 9
## 6 | 4
## 8 | 1
## 10 | 0
29
Kernel Density Curve
I Surprisingly, R does not have a specific function to plotdensity curves
I R does have the density() function which computes akernel density estimate
I We can pass a "density" object to plot() in order toget a density curve.
30
Kernel Density Curve
# kernel density curve
dens <- density(num_vec)
plot(dens)
−50 0 50 100 150
0.00
00.
004
0.00
8
density.default(x = num_vec)
N = 10 Bandwidth = 19.41
Den
sity
31
Test your knowledge
What type of plot is based on the five-numbersummary
A) bar chart
B) box plot
C) histogram
D) scatterplot
32
Plots of Two Variables
33
High-level graphics of two variables
Function Data Descriptionplot() numeric, numeric scatterplotplot() numeric, factor stripchartsplot() factor, numeric boxplotsplot() factor, factor spineplotplot() 2-column numeric matrix scatterplotplot() 2-column numeric data.frame scatterplotplot() 2-D table mosaicplot
34
Two variable objects
2-D table(frequency orcrosstable)
2-column (numeric data.frame)
2-column (numeric matrix)
2 numeric vectorsnum vector, factorfactor, num vector2 factors
35
Plots of two variables
# plot numeric, numeric
plot(iris$Petal.Length, iris$Sepal.Length)
# plot numeric, factor
plot(iris$Petal.Length, iris$Species)
# plot factor, numeric
plot(iris$Species, iris$Petal.Length)
# plot factor, factor
plot(iris$Species, iris$Species)
36
Plots of two variables
# plot numeric, numeric
plot(iris$Petal.Length, iris$Sepal.Length)
●●
●●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●
●
●
●
●
●●●
●●
●●
●●
●
●●
●
●
●
●●
●●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●●
●
●
●
●●
●
●
●●
●●
●●
●●
●
●●●
●●
●
●
●
●
●● ●
●
●
●
●●●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
●●
●●
● ●
●
●
●
●
●
●
●
●●
●
●●
●
●●
●
●
●●
●
●●
●
●
●●●
●●
●
●
1 2 3 4 5 6 7
4.5
5.5
6.5
7.5
iris$Petal.Length
iris
$S
ep
al.L
en
gth
37
Plots of two variables
# plot numeric, factor
plot(iris$Petal.Length, iris$Species)
●●● ●● ●●●●●●●●●● ●●● ●● ●●● ● ●●●●● ●●●●●●●●●● ●●●● ● ●● ●●●●
●● ●● ●● ●● ●●● ●● ●● ●●● ●● ●● ●●●● ● ●●● ●● ● ●●● ●●●● ● ●●● ●●●●● ●
●● ●● ● ●● ●● ●● ● ●●● ● ● ● ●● ●● ●● ● ●●● ● ● ● ●●● ● ●●●● ● ●●● ●●●● ● ●●
1 2 3 4 5 6 7
1.0
1.5
2.0
2.5
3.0
iris$Petal.Length
iris
$S
pe
cie
s
38
Plots of two variables
# plot factor, numeric
plot(iris$Species, iris$Petal.Length)
●
●
setosa versicolor virginica
12
34
56
7
39
Plots of two variables
# plot factor, factor
plot(iris$Species, iris$Species)
x
y
setosa versicolor virginica
seto
save
rsic
olor
virg
inic
a
0.0
0.2
0.4
0.6
0.8
1.0
40
Plots of two variables
# some fake data
set.seed(1)
# hair color
hair <- factor(
sample(c('blond', 'black', 'brown'), 100, replace = TRUE))
# eye color
eye <- factor(
sample(c('blue', 'brown', 'green'), 100, replace = TRUE))
41
Plots of two variables
# plot factor, factor
plot(hair, eye)
x
y
black blond brown
blue
brow
ngr
een
0.0
0.2
0.4
0.6
0.8
1.0
42
More high-level graphics of two variables
Function Data Descriptionsunflowerplot() numeric, numeric sunflower scatterplotsmoothScatter() numeric, numeric smooth scatterplot
boxplot() list of numeric boxplotsbarplot() matrix stacked / side-by-side barplotdotchart() matrix dotplot
stripchart() list of numeric stripchartsspineplot() numeric, factor spinogramcdplot() numeric, factor conditional density plot
fourfoldplot() 2x2 table fourfold displayassocplot() 2-D table association plotmosaicplot() 2-D table mosaic plot
43
Plots of two variables
# sunflower plot (numeric, numeric)
sunflowerplot(iris$Petal.Length, iris$Sepal.Length)
1 2 3 4 5 6 7
4.5
5.5
6.5
7.5
iris$Petal.Length
iris
$S
ep
al.L
en
gth
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●●
●
●
● ●
●● ●
●●
●●
●
●
●●
●●
●●●
●
●
●
●
●
●
●●
●
●
●●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●●
●
●
●●●●
●
●●
●
●
●
●●
●
●●
●
●
●●●
●
●
44
Plots of two variables
# smooth scatter plot (numeric, numeric)
smoothScatter(iris$Petal.Length, iris$Sepal.Length)
1 2 3 4 5 6 7
4.5
5.5
6.5
7.5
iris$Petal.Length
iris
$S
ep
al.L
en
gth
45
Plots of two variables
# boxplots (numeric, numeric)
boxplot(iris$Petal.Length, iris$Sepal.Length)
1 2
12
34
56
78
46
Plots of two variables
m <- matrix(1:8, 4, 2)
# barplot (numeric matrix)
barplot(m)
05
10
15
20
25
47
Plots of two variables
m <- matrix(1:8, 4, 2)
# barplot (numeric matrix)
barplot(m, beside = TRUE)
02
46
8
48
Plots of two variables
# conditional density plot (numeric, factor)
cdplot(iris$Petal.Length, iris$Species)
iris$Petal.Length
iris
$S
pe
cie
s
2 3 4 5 6
seto
savi
rgin
ica
0.0
0.2
0.4
0.6
0.8
1.0
49
Two categorical variables: frequency table
# 2-D table (HairEyeColor data)
x <- margin.table(HairEyeColor, c(1, 2))
x
## Eye
## Hair Brown Blue Hazel Green
## Black 68 20 15 5
## Brown 119 84 54 29
## Red 26 17 14 14
## Blond 7 94 10 16
50
Plots of two categorical variables
# mosaic plot (2-D table)
mosaicplot(x, main = "Relation between hair and eye color")
Relation between hair and eye color
Hair
Eye
Black Brown Red BlondB
row
nB
lue
Ha
zel
Gre
en
51
Plots of two categorical variables
# association plot (2-D table)
assocplot(x, main = "Relation between hair and eye color")
Black Brown Red Blond
Gre
en
Blu
eB
row
nRelation between hair and eye color
Hair
Eye
52
Plots of Multiple Variables
53
High-level graphics of multiple variables
Function Data Descriptionplot() data frame scatterplot matrixpairs() matrix scatterplot matrixmatplot() matrix scatterplotstars() matrix star plots
image() numeric, numeric, numeric image plotcontour() numeric, numeric, numeric contour plotfilled.contour() numeric, numeric, numeric filled contour plotpersp() numeric, numeric, numeric 3-D surfacesymbols() numeric, numeric, numeric symbols scatterplot
mosaicplot() N-D table mosaic plot
54
Plots of multiple variables
# scatter plot matrix (data frame)
plot(iris[ , 1:4])
## Warning: closing unused connection 5
(http://gastonsanchez.com/education.csv)
Sepal.Length
2.0 3.0 4.0
●●
●●
●
●
●
●
●
●
●
●●
●
● ●●
●
●
●●
●
●
●●
● ●●●
●●
●●●
●●
●
●
●
●●
● ●
● ●●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●● ●
●
●
●●
●
●●
●●
●●●
● ●
●●
●●●●
●
●
●
●
●●●
●●
●
● ●●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
● ●
●●
●●
●
●
●
●
●
●
●
● ●●
●●
●
●●●
●
●●
●
●●●
●
●●●
●●
●●
●●●●
●
●
●
●
●
●
●
●●
●
●●●●
●
●●
●
●
●●
●●●●
●●
●●●
●●
●
●
●
●●
●●
●●●●
●
●●
●
●
●
●
●
●
●
●
●
●●
●●●
●
●
●●
●
●●
●●
●●●
●●
●●
●●●
●
●
●
●
●
●●●
●●
●
●●●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
●●
●●
●●
●
●
●
●
●
●
●
●●●
●●
●
●●●
●
●●
●
●●
●
●
●●●
●●●
●
0.5 1.5 2.5
4.5
6.0
7.5
●●●●
●
●
●
●
●
●
●
●●
●
● ●●
●
●
●●
●
●
●●● ●●●
●●
●●●
●●
●
●
●
●●
●●
●●●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●● ●
●
●
●●
●
●●
●●
●●●● ●
●●●●●
●
●
●
●
●
●●●
●●
●
●●●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
● ●
●●
●●
●
●
●
●
●
●
●
●●●
●●
●
●●●
●
●●
●
●●
●
●
● ●●
●●
●●
2.0
3.0
4.0
●
●●●
●
●
● ●
●●
●
●
●●
●
●
●
●
●●
●
●●
●●
●
●●●●●
●
●●
●●
●●
●
●●
●
●
●
●
●
●
●
●
●●● ●
●
●●
●
●
●●
●
●
●
●●●●
●
●
●
●
●
●
●●●
●●●
●●●
●●
●
●
●
●
●
●●
●
●
●
●
●● ●
●
●
●
●
●●● ●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
● ●●
●●
●●
●●
●
●
●●●
●
●
●● ●●●
●
●●
●
●
●
●
●Sepal.Width
●
●●●
●
●
●●
●●
●
●
●●
●
●
●
●
●●
●
●●
●●
●
●●●●●
●
●●
●●
●●
●
●●
●
●
●
●
●
●
●
●
●●●●
●
●●
●
●
●●
●
●
●
●●●●
●
●
●
●
●
●
●●●
●●●
●●●
● ●
●
●
●
●
●
●●
●
●
●
●
●●●
●
●
●
●
●●● ●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
● ●●
●●
●●
●●
●
●
●●●
●
●
●● ●●●
●
●●
●
●
●
●
●
●
●●●
●
●
●●
●●
●
●
●●
●
●
●
●
●●
●
●●
●●
●
●●●●●
●
●●
●●
●●
●
●●
●
●
●
●
●
●
●
●
●●●●
●
●●
●
●
●●
●
●
●
●●●●
●
●
●
●
●
●
●●●●
●●
●●●
● ●
●
●
●
●
●
●●
●
●
●
●
●●●
●
●
●
●
●● ●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●●●
●●
●●
●●
●
●
●●●
●
●
●● ● ●●
●
●●
●
●
●
●
●
●●●● ●●
● ●● ● ●●●● ●
●●●●● ●●
●
●●●●●●●● ●● ●●● ●●● ●●●●●●
● ●● ●●
●●●
●●● ●
●
●
●●
●●
●
●
●●●
●●
●
●
●●●●
●●●
●●● ●
●● ● ●
●●●
● ●●
●
●●● ●
●
●
●
●
●●●
●
●
●●
●
●●●
●●●●
●●
●
●
●
●
●
●●
●●
● ●●
●
●●
●●
●●
●●●●●
●●●●●●
●
●● ●● ●●
●●● ● ●●●● ●
●●●●●● ●
●
●●● ●●●●● ● ●●●
● ●●● ●●● ●●
●● ●● ●●
●●●
●●● ●
●
●
●●
●●
●
●
●●●
●●
●
●
● ●●●
● ●●
●●● ●
●● ●●
●●●
● ●●
●
● ●●●
●
●
●
●
●●●
●
●
●●
●
●● ●● ● ●●
●●
●
●
●
●
●
●●
● ●
● ●●
●
●●
●●
●●
●●●●●
●●●● ● ●●
Petal.Length
13
57
●●●●●●
●●●●●●●●●
●●●●●● ●
●
●●● ●●●●● ●●●●●●●●●●●●
●●
●●●●●
●●●
●●● ●
●
●
●●
●●
●
●
●●●
●●
●
●
●●●●● ●●
●●● ●
●●●●
●●●
● ●●
●
●●●●
●
●
●
●
●● ●
●
●
●●
●
●● ●● ●●●
●●
●
●
●
●
●
●●
●●
●●●●
●●
●●●●
●● ●
●●
● ●●●● ●
●
4.5 6.0 7.5
0.5
1.5
2.5
●●●● ●●●
●● ● ●●●● ●●●● ●●
●●
●
●
●●●
●●●●●
● ●●● ●●● ●●●
●
●●●●● ●●
●● ●●
●●
●
●
●●
●
●
●
●● ●●
●
●
●
●
●●
● ●●●
●●
●●●●
●●
●●
●●●●●
●●
●●● ●●
●
●
●●
●
● ●
● ●●
●
●●●
●
● ●
●
●●
●
●
● ●●
●
●●●
●
●
● ●●
●●
●●
●●
●
●●
●
●●●
●●
●
●
●● ●● ●●●
●● ● ●●●● ●●●● ●●
●●
●
●
●●●●●●●
●
●●●● ●●● ●●●
●
●●●●● ●●
●●●●
●●
●
●
●●
●
●
●
●● ●●
●
●
●
●
●●
●●●●
●●
●●●●
●●
●●
● ●●●●
●●
● ●●●●
●
●
●●
●
●●
● ●●
●
●●●
●
● ●
●
●●
●
●
●●●
●
●● ●
●
●
● ●●
●●
● ●
●●
●
●●
●
●●
●
● ●
●
●
1 3 5 7
●●●●●●●
●●●●●●●●●●●●●●
●●
●
●●●●●●●●
●●●●●●●●●●●
●●●
●●●●
●●●●
●●
●
●
●●
●
●
●
●● ●●
●
●
●
●
●●
●●●●
●●
●●●●
●●●●
●●●●●
●●
●●●●●
●
●
●●
●
● ●
● ●●
●
●●●
●
●●
●
●●
●
●
● ●●
●
●●●
●
●
●●●
● ●
●●
●●
●
●●
●
●●
●
●●
●
●
Petal.Width
55
Plots of multiple variables
# scatter plot matrix (data frame)
pairs(iris[ , 1:4])
Sepal.Length
2.0 3.0 4.0
●●
●●
●
●
●
●
●
●
●
●●
●
● ●●
●
●
●●
●
●
●●
● ●●●
●●
●●●
●●
●
●
●
●●
● ●
● ●●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●● ●
●
●
●●
●
●●
●●
●●●
● ●
●●
●●●●
●
●
●
●
●●●
●●
●
● ●●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
● ●
●●
●●
●
●
●
●
●
●
●
● ●●
●●
●
●●●
●
●●
●
●●●
●
●●●
●●
●●
●●●●
●
●
●
●
●
●
●
●●
●
●●●●
●
●●
●
●
●●
●●●●
●●
●●●
●●
●
●
●
●●
●●
●●●●
●
●●
●
●
●
●
●
●
●
●
●
●●
●●●
●
●
●●
●
●●
●●
●●●
●●
●●
●●●
●
●
●
●
●
●●●
●●
●
●●●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
●●
●●
●●
●
●
●
●
●
●
●
●●●
●●
●
●●●
●
●●
●
●●
●
●
●●●
●●●
●
0.5 1.5 2.5
4.5
6.0
7.5
●●●●
●
●
●
●
●
●
●
●●
●
● ●●
●
●
●●
●
●
●●● ●●●
●●
●●●
●●
●
●
●
●●
●●
●●●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●● ●
●
●
●●
●
●●
●●
●●●● ●
●●●●●
●
●
●
●
●
●●●
●●
●
●●●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
● ●
●●
●●
●
●
●
●
●
●
●
●●●
●●
●
●●●
●
●●
●
●●
●
●
● ●●
●●
●●
2.0
3.0
4.0
●
●●●
●
●
● ●
●●
●
●
●●
●
●
●
●
●●
●
●●
●●
●
●●●●●
●
●●
●●
●●
●
●●
●
●
●
●
●
●
●
●
●●● ●
●
●●
●
●
●●
●
●
●
●●●●
●
●
●
●
●
●
●●●
●●●
●●●
●●
●
●
●
●
●
●●
●
●
●
●
●● ●
●
●
●
●
●●● ●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
● ●●
●●
●●
●●
●
●
●●●
●
●
●● ●●●
●
●●
●
●
●
●
●Sepal.Width
●
●●●
●
●
●●
●●
●
●
●●
●
●
●
●
●●
●
●●
●●
●
●●●●●
●
●●
●●
●●
●
●●
●
●
●
●
●
●
●
●
●●●●
●
●●
●
●
●●
●
●
●
●●●●
●
●
●
●
●
●
●●●
●●●
●●●
● ●
●
●
●
●
●
●●
●
●
●
●
●●●
●
●
●
●
●●● ●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
● ●●
●●
●●
●●
●
●
●●●
●
●
●● ●●●
●
●●
●
●
●
●
●
●
●●●
●
●
●●
●●
●
●
●●
●
●
●
●
●●
●
●●
●●
●
●●●●●
●
●●
●●
●●
●
●●
●
●
●
●
●
●
●
●
●●●●
●
●●
●
●
●●
●
●
●
●●●●
●
●
●
●
●
●
●●●●
●●
●●●
● ●
●
●
●
●
●
●●
●
●
●
●
●●●
●
●
●
●
●● ●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●●●
●●
●●
●●
●
●
●●●
●
●
●● ● ●●
●
●●
●
●
●
●
●
●●●● ●●
● ●● ● ●●●● ●
●●●●● ●●
●
●●●●●●●● ●● ●●● ●●● ●●●●●●
● ●● ●●
●●●
●●● ●
●
●
●●
●●
●
●
●●●
●●
●
●
●●●●
●●●
●●● ●
●● ● ●
●●●
● ●●
●
●●● ●
●
●
●
●
●●●
●
●
●●
●
●●●
●●●●
●●
●
●
●
●
●
●●
●●
● ●●
●
●●
●●
●●
●●●●●
●●●●●●
●
●● ●● ●●
●●● ● ●●●● ●
●●●●●● ●
●
●●● ●●●●● ● ●●●
● ●●● ●●● ●●
●● ●● ●●
●●●
●●● ●
●
●
●●
●●
●
●
●●●
●●
●
●
● ●●●
● ●●
●●● ●
●● ●●
●●●
● ●●
●
● ●●●
●
●
●
●
●●●
●
●
●●
●
●● ●● ● ●●
●●
●
●
●
●
●
●●
● ●
● ●●
●
●●
●●
●●
●●●●●
●●●● ● ●●
Petal.Length
13
57
●●●●●●
●●●●●●●●●
●●●●●● ●
●
●●● ●●●●● ●●●●●●●●●●●●
●●
●●●●●
●●●
●●● ●
●
●
●●
●●
●
●
●●●
●●
●
●
●●●●● ●●
●●● ●
●●●●
●●●
● ●●
●
●●●●
●
●
●
●
●● ●
●
●
●●
●
●● ●● ●●●
●●
●
●
●
●
●
●●
●●
●●●●
●●
●●●●
●● ●
●●
● ●●●● ●
●
4.5 6.0 7.5
0.5
1.5
2.5
●●●● ●●●
●● ● ●●●● ●●●● ●●
●●
●
●
●●●
●●●●●
● ●●● ●●● ●●●
●
●●●●● ●●
●● ●●
●●
●
●
●●
●
●
●
●● ●●
●
●
●
●
●●
● ●●●
●●
●●●●
●●
●●
●●●●●
●●
●●● ●●
●
●
●●
●
● ●
● ●●
●
●●●
●
● ●
●
●●
●
●
● ●●
●
●●●
●
●
● ●●
●●
●●
●●
●
●●
●
●●●
●●
●
●
●● ●● ●●●
●● ● ●●●● ●●●● ●●
●●
●
●
●●●●●●●
●
●●●● ●●● ●●●
●
●●●●● ●●
●●●●
●●
●
●
●●
●
●
●
●● ●●
●
●
●
●
●●
●●●●
●●
●●●●
●●
●●
● ●●●●
●●
● ●●●●
●
●
●●
●
●●
● ●●
●
●●●
●
● ●
●
●●
●
●
●●●
●
●● ●
●
●
● ●●
●●
● ●
●●
●
●●
●
●●
●
● ●
●
●
1 3 5 7
●●●●●●●
●●●●●●●●●●●●●●
●●
●
●●●●●●●●
●●●●●●●●●●●
●●●
●●●●
●●●●
●●
●
●
●●
●
●
●
●● ●●
●
●
●
●
●●
●●●●
●●
●●●●
●●●●
●●●●●
●●
●●●●●
●
●
●●
●
● ●
● ●●
●
●●●
●
●●
●
●●
●
●
● ●●
●
●●●
●
●
●●●
● ●
●●
●●
●
●●
●
●●
●
●●
●
●
Petal.Width
56
Plots of multiple variables
# scatter plot matrix (data frame)
matplot(iris[ , 1:4])
111111
11111111
1111111111111
111111111111
1111111
11
11
111
1
1
11
1
1
11
1111
1
111111111111
1111
1111
11
11111
1111
1
1111
1
11
1
1
111
111
1111
11
1
1
1
1
111
111
111
111
1
111
111
1
111111
1
0 50 100 150
02
46
8
iris
[, 1
:4]
2222
2222222222
22222222222222222
2
22
2222222
2
222
2
2222222
2222
222
2
2
2
2222222
222222222222
22222
2
2222222222
22222222
222
22222222
2
22
2222
2222222
2
2222222222
2222
2222
3333333333333
33333
33333
33333333333333333333
3333333
333
3333
3
3
33
33
3
3
33333
3
3
3333333
3333
33333
33333
3
3333
3
3
3
3
333
3
3
333
3333333
33
3
3
3
3
3
33
33
3333
333333
33333
33333
33
44444444444444444444444
44444444444444444444
4444444
44444444444444444
444
4444444
444444
44444444444444444
4444
44444
44444
44444
4
4444
4444
44444
44
44444444444
4444
57
Plots of multiple variables
# star plot (data frame)
stars(iris[ , 1:4])
58
Plots of multiple variables
# color image (matrix)
image(t(volcano)[ncol(volcano):1, ])
0.0 0.2 0.4 0.6 0.8 1.0
0.0
0.2
0.4
0.6
0.8
1.0
59
Plots of multiple variables
# display of Maunga Whau volcano
x <- 10*(1:nrow(volcano))
y <- 10*(1:ncol(volcano))
image(x, y, volcano, col = terrain.colors(100), axes = FALSE)
contour(x, y, volcano, levels = seq(90, 200, by = 5),
add = TRUE, col = "peru")
axis(1, at = seq(100, 800, by = 100))
axis(2, at = seq(100, 600, by = 100))
box()
title(main = "Maunga Whau Volcano", font.main = 4)
60
Plots of multiple variables
x
y
95
100
100
100
105
105 105
110
110
110
110
115
115 115
120 125
130
135
140 145
150
155
155
160
160
165
165
170
170 175
180
185
190
100 200 300 400 500 600 700 800
10
02
00
30
04
00
50
06
00
Maunga Whau Volcano
61
Plots of multiple variables# mosaic plot of N-D tables
mosaicplot(HairEyeColor)
HairEyeColor
Hair
Eye
Black Brown Red BlondB
row
nB
lue
Ha
zel
Gre
en
MaleFemale Male Female MaleFemale Male Female
62
Plots of multiple variables# symbols scatter plots
symbols(iris[, 1], iris[, 2], circles = iris[, 3]/100,
inches = FALSE)
4 5 6 7 8
2.0
3.0
4.0
iris[, 1]
iris
[, 2] ●
●
●
●
●
●
● ●
●
●
●
●
●●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●●
●
●●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●●
●
●
●●
●
●●
● ●
●
●
●
●
●
●●
●
●
●
●
●● ●
●
●
●
●
●●
● ●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
● ●●
●●
●
●
●●
●
●
●●
●
●
●
●●
●● ●
●
●●
●
●
●
●
●
63
Graphics Parameters
Graphics Functions and ArgumentsI Plot functions usually come with various arguments
I Typically, the first argument(s) is the data object(s) to beplotted
I Most of the other arguments have default options
I Graphic arguments have a consisting naming convention,but there will always be some exception
64
Graphical Parameters
Graphical ArgumentsI Some arguments are specific to a function (e.g. horiz orbeside in barplot())
I Other arguments are more general (e.g. col, xlab, ylab)
I General graphical parameters are listed in thedocumentation of the function par()
I See ?par for more information
65
Graphics in R
How to choose a graphics approach?I look first for an existing function that does what you want
—or something similar to what you want (don’t reivent thewheel!)
I Existing plotting functions can be combined andcustomized by using optional arguments or graphicalparameters
I For exploratory data analysis (quick and dirty) the plottingfunctions in "graphics" is a good option
66