Data Visualization With Ggplot2: Sca!er Plots
Data Visualization With Ggplot2: Sca!er Plots
Sca!er Plots
Data Visualization with ggplot2
37 Geometries
abline density2d line rect vline
area dotplot linerange ribbon
bar errorbar map rug
bin2d errorbarh path segment
blank freqpoly point smooth
boxplot hex pointrange step
contour histogram polygon text
crossbar hline quantile tile
density ji!er raster violin
Data Visualization with ggplot2
Sca!er Plot
4.5
●
●
●
4.0 ●
●
● ● ● ●
● ● ●
Sepal.Width
3.5 ● ● ● ●
● ● ● ● ● ● ● ● ●
● ● ●●
● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ●
3.0 ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ● ●
● geom_point()
● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ● ● ● ● ● ●
● ● ● ● ● ●
● ● ● ● ●
2.5 ● ● ● ● ● ● ●
● ●
● ● ● ●
● Essential: x, y 2.0 ●
● ●
5 6 7 8
Sepal.Length
Sca!er Plot
4.5
●
●
●
4.0 ●
●
● ● ● ●
● ●●
Sepal.Width
3.5 ●●● ● Species
● ● ●●● ● ● ●●
●● ● ● ● Setosa
● ●● ● ● ●● ●●● ●
● ●● ● ● ●
● Versicolor
3.0 ●● ●●● ● ●● ●●● ●●●● ●● ●●
Virginica
geom_point()
●
●
● ●● ●●●●● ● ●
●●● ●●●●● ● ● ●
● ● ● ● ●●
● ●● ● ●
2.5 ● ● ●●● ● ●
● ●
● ● ● ●
● Essential: x, y 2.0 ●
● ●
5 6 7 8
Sepal.Length
● Optional: alpha, colour, fill, shape, size
4.5
●
control aesthetic mappings
of each layer independently
●
●
4.0 ●
●
● ● ● ●
● ●●
● ●● ●
Sepal.Width
2.0 ●
5 6 7 8
Sepal.Length
Data Visualization with ggplot2
Summary Statistics
> head(iris)
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
1 Setosa 5.1 3.5 1.4 0.2
2 Setosa 4.9 3.0 1.4 0.2
3 Setosa 4.7 3.2 1.3 0.2
4 Setosa 4.6 3.1 1.5 0.2
5 Setosa 5.0 3.6 1.4 0.2
6 Setosa 5.4 3.9 1.7 0.4
Add Layers
> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
4.5
●
●
●
4.0 ●
●
● ● ● ●
● ●●
● ●● ●
Sepal.Width
2.0 ●
5 6 7 8
Sepal.Length
Data Visualization with ggplot2
shape ~ pch
0 ● 1 2 3 4
5 6 7 8 9
● 10 11 12 ● 13 14
15 ● 16 17 18 ● 19
● 20 ● 21 22 23 24 25
Example
> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
geom_point() +
geom_point(data = iris.summary,
shape = 21, size = 5, fill = "#00000080")
4.5
●
●
●
4.0 ●
●
● ● ● ●
● ●●
● ●● ●
Sepal.Width
2.0 ●
5 6 7 8
Sepal.Length
Data Visualization with ggplot2
Crosshairs
> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
geom_point() +
geom_vline(data = iris.summary)
4.5
●
●
●
4.0 ●
●
● ● ●●
● ●●
● ●● ●
Sepal.Width
2.0 ●
0 2 4 6 8
Sepal.Length
Data Visualization with ggplot2
Crosshairs
> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
geom_point() +
geom_vline(data = iris.summary, aes(xintercept = Sepal.Length))
4.5
●
●
●
4.0 ●
●
● ● ● ●
● ●●
● ●● ●
Sepal.Width
2.0 ●
5 6 7 8
Sepal.Length
Data Visualization with ggplot2
Crosshairs
> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
geom_point() +
geom_vline(data = iris.summary, aes(xintercept = Sepal.Length)) +
geom_hline(data = iris.summary, aes(yintercept = Sepal.Width))
4.5
●
●
●
4.0 ●
●
● ● ● ●
● ●●
● ●● ●
Sepal.Width
2.0 ●
5 6 7 8
Sepal.Length
Data Visualization with ggplot2
Crosshairs
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
geom_point() +
geom_vline(data = iris.summary, linetype = 2,
aes(xintercept = Sepal.Length, col = Species)) +
geom_hline(data = iris.summary, linetype = 2),
aes(yintercept = Sepal.Width, col = Species),
4.5
●
0
●
●
4.0 ●
● 1
● ● ● ●
● ●●
● ●● ●
Sepal.Width
2.0 ●
5 6 7 8 6
Sepal.Length
Course Title
Remarks
● ggplot2 can also calculate statistics
● mean only = no good
4.5
●
●
●
4.0 ●
●
● ● ● ●
● ●●
● ●● ●
Sepal.Width
2.0 ●
5 6 7 8
Sepal.Length
Data Visualization with ggplot2
Ji!er
> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
geom_point(position = "jitter")
4.5
●
●
●
4.0 ●
●●
●
● ● ●
● ●
● ● ●
●
● ●●
Sepal.Width
3.5 ●
● ●
●● ● Species
● ● ● ● ● ●
● ● ● ●
● ● ●
●● ● ●
●
● Setosa
● ● ●● ●
● ●●● ● ● ●
● ● ●
● ● ●●
● ●
●
● Versicolor
3.0 ●●
● ●
● ● ●●● ●● ● ● ●●
● ● ●
●● ● ● ●●● ● ●
● ●
● ● ●● ●
●●
● ● Virginica
●
● ● ● ● ●
● ●● ●● ● ●
● ● ● ●
● ● ●
●
● ● ● ●
● ●
● ● ●
2.5 ● ●● ●
●●
●
●
● ●
● ●
●
● ●
2.0 ●
5 6 7 8
Sepal.Length
Data Visualization with ggplot2
Ji!er
> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
geom_jitter()
●
●
●
4.0 ●
●
●
●
● ● ● ●
●●
●
● ●
● ●
3.5 ●
●
Sepal.Width
●
●
●
●
● ● Species
● ● ●
● ● ● ●●
● ●
● ●
●
●●
● ●
● ● Setosa
● ●● ● ●
● ●● ● ●●
●
● ●
● ● ●
●● ●●
●
● Versicolor
●
3.0 ●● ●● ● ● ●
● ● ● ● ● ●● ●
●
● ●● ● ●● ●
●
●
●
●
● ● ●● ● ● ● Virginica
● ● ● ● ● ●
●●●● ● ●
● ●
● ●●● ●
● ● ● ●
● ● ●
● ●
●
2.5 ●
●●
● ●● ●
● ●
●
● ●
● ●
●
● ●
2.0 ●
5 6 7 8
Sepal.Length
Data Visualization with ggplot2
Ji!er
> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
geom_jitter(alpha = 0.6)
4.5
4.0
Sepal.Width
3.5 Species
Setosa
Versicolor
3.0
Virginica
2.5
2.0
5 6 7 8
Sepal.Length
Data Visualization with ggplot2
Ji!er
> ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
geom_jitter(shape = 1)
4.5
●
●
●
4.0 ●
●
●● ● ● ●
● ● ●
● ● ● ●
Sepal.Width
3.5 ●
●●● ●
● Species
●● ● ●
● ●● ●● ● ●●
● ● ●
●
● ● Setosa
● ●
●●● ● ● ● ●● ●
● ● ●
● ●●● ● ● ●●
●
● ● Versicolor
● ● ●● ● ● ●●
3.0 ● ● ●● ● ● ● ● ●●● ●● ●● ● ●● ● Virginica
● ● ● ●● ● ● ● ●
● ● ●● ● ●
● ● ● ● ●
● ● ● ●
●
● ● ● ● ●
●
● ● ●
● ●
2.5 ●
● ●● ● ●●
●
● ●
●
● ● ●
●
●●
●
2.0 ●
5 6 7 8
Sepal.Length
Data Visualization with ggplot2
Diamonds
> ggplot(diamonds, aes(carat, price)) +
geom_point()
● ●●●●●
●●● ●
●
● ● ●●
●
●●
●
●
●●
●
●●●
●
●●●
●●●
●●
●● ●●● ● ●
●
● ● ●
●
●●● ●●
●●
● ●
●
●
●●●
●
●●
●● ●●
●●
●
●●
●●
●
●
●
●●
●
●
●
●●
●
●
●●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●●●● ●
● ●● ● ●●
●
●● ●
●
●● ●●●●
●●
●
●●●
●●
●
●●●●
●
●●
●●●
●●●●
●
●●●
●● ●
●●●● ●●●●● ●
●●
●
●
●● ●
●●
●
●●
●
●●
●●
●
●●
●●
●
●●
●●●
●●●●●●●●
●●●
●
●●
●●
●● ●
●
●●● ●●●
●
●
●
●●●
●
●●
●●
● ●●
●
● ●
●●
●●●
●●●
● ●● ●
●● ● ●● ●
●
●●●●
●●
● ●
●
●●
● ● ●
●●
●●
●●
●
●
●●●
●
●●
●●
●
●●
●●●
●●
● ●
●●●
● ●
●●● ●
● ●● ●
●
●●● ●
●
●
●●
●
● ●●●
●
●
●
●●
●
●●
●●
●●
●●
●●
●
●●●●●
●●●
● ●● ●
●●●
●●
●● ●
●
●● ● ●
●●
●
●●
●
●●●
●●
●
●● ●●
●●●
● ●
●
●●●
● ● ●
● ●
●
●
●●
●
●
●
●
●
●●
●
●●
● ●
●●●
● ●●●●
●●
●●
●
●●
●
●
●
●●
●
●●
●●●
●
●●
●
●
●
●
●●
●●
●
●
●
●
●●●
●
●●●●
●●●
● ●
●● ●
● ● ●
●●
● ●
●● ●●●●●
●●
●● ●●
●
●● ● ● ● ● ●
●●●
● ●●●
● ●
●●●●●
●●
●●
●●
●
●
●●
●●
●
●
●●
●
●● ●●●
●
●
●●
●
●●
●●
●
●
●●
●●
●
●
●
●
●
●●
●
●●
●
●
●●●●●
● ●●
● ●● ●
●●●
●● ● ●●●
● ●
●●
●●●
●●●
● ●
●
●
●●● ●● ●●
●
●
●
●
●
●●
●
●●
●●
●●●
●
●
●●●
●●● ●●●
●
●
●●
●●
●●●
●
●●
●●
●●
●●
●●● ●
●●
●
●
●● ●● ● ●
15000 ●●●
●
●
●●
●●
●
●
●
●
●●●
●●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
● ●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●● ●
●●●●●
●
●
● ●
● ●
●
●●●●●●●
●
●●
●
●
●
●●●●
●
●
●●
●● ●●
●
● ●
●
●●
●
●●
●
●●●
●
●●
●
●
●●
●
● ●
● ●●
● ●
●●● ●
●● ●●●
●
●●●
●
●
●●
●●●
●
●●
●●●
●● ●●●
●●
●
●●
●
●●
●
●
●●
●●●●
●●
●
●
● ●●●
●
●
●
●
●
●
●●●
● ●●●
●
●
●
●
●
●
●
●
●
●●
●●
●●
●
●
●
●
●
●
●
●
●●●
●
●●
● ●
●
●
●
●
●
●●
●
●
●●
●
●●
●
●
●
●●●
●
●
●●
●
●
●
●
●
●●
●
●
●
●
●
●
● ●
●●● ●●●●
●●
●
● ●●
●
●
●●
●●●●●
●●
●●
●
● ●●●
●●●
●●
●●●
●
●●
●●
●
●
●● ●●● ●
●
●●●●●●
●●
●
●●
●
●● ● ●
●
●
●●
●
●
●●
●
●●
● ●●
●
●
●
●●●
●
●●● ●
●
●●
●●
● ●
●
●
●●
●
●
●●●
●●
●
●●●
● ●● ●
● ●●●
●●●
●●●●
●●
● ●
●
●●
●
●●
●
●●
●●
●●
●●
●
●
●
●●
●●●
● ●●
●●
● ●
●
●
●
●●
●●●
●●
●●
●●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●●
●
●
●●
●
●●
●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●●●●
●●
●
●
●
●●
●
●
●●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
● ●
●●● ● ●
●
●
●●●●
●
●●
●●
●●●
●
●●●
●
●
●●●
●
●●●●
●
●
●●
●
●●
●
●●
●●
●
●●
●
●●●
●
●●
●
●●●
●
● ●●●
●
●●
●
●
●●
●●
●
●● ● ●
●
●
●●
●●●●
●●
●
●●
●●●●
●●●
●
●●
●●●
●
●●
●
●
●●
●●
●●
●
●●
●●
● ●● ● ●
●●● ●
●
●
●
●
●
●
●
●●●●
●
●●●
●
●●
●
●
●●
●●
●
●●●
●
●●
●
●●
●●
●●
●●●
●
●
●●
●
●
●●
●●
●●
●●
●●●
●
●
●
●●●●●●●
●●●
●
●●●●● ● ● ● ●
●
●
●
●●●●
●
●●
●●●
●
●
●●
●
●
●●●
●
●●
●
●●
●
● ●
●
●●
●
●
●
●●
●●
●●●●
●
●
●●●●
●●
●
●
●●
●●●
●● ●
●●
●
●●
●
● ● ●
●
●●
●●●●●
●●
●
●●●
●
●
●●
●●
●●
●
●
●●
●●
●
●●
●
●●
●●
●
●●
●● ●●
● ●●●
● ●●●
●
●
●●
●●
●
●
●
●
●●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
●●
●●●●
●
●
●●
●
●
●
●●
●●
●
●●
●●
●●
●
●●
●
●
●●
●
●
●
●
●
●
●●
●
●●
●●●●●
●●●●
●●● ●●● ●
●
●●●●
●●●
●
●●
●● ●●
●
● ●●
●
●●
●
●●
●●●
●● ● ●● ●
price
●
●
●
●●
●●
●●●
●
●●●●
●●
●●
●
●●●
●
●●
●
●●
●●
●
●●
●●●●
●●
●●●
●
●
●●
● ●
●●
●●
●●
●
●●● ●
●
●●
● ● ●●●
●●●● ●● ●
●
● ●●
● ●●
●
● ●
● ●
●● ● ●
10000 ●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
●●
●●
●
●
●
●●
●●
●●
●
●
●
●
●
●
●
●
●
●
●●●
● ●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●●
●
●●
●●
●
●
●●●
●
●●
●
●●
●●
●
●●
●●
●
●
●●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●●●
●
●
●
●
●
●
●
●
●
● ● ●●
●
●
●
●
●
●
●●
●
●●
●
●
●
●●
●
●
●●
●
●●
●
●●
●
●
●●
●
●
●●
●●●
●
●
●
●●
●●
●●
●
●
●●
●
●
●●
●
●●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●●
●●
●●
●
●●
●●
●
●
●
●
●●●●●●
●
●
●
●●●
●
●●
●
●●
●●
●●●
●●
●●
●
●●
●
●●
●●
●
●●
●●
●
●●
●
●●
● ●
●
●
●●
●
●
●●
●●
●●
●●●
●●●●● ● ●● ●
●●●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●
●
●
●
●
●●
●●
●
●●
●●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●●
●●
●●
●●
●●
●●
●●
●●
●●●
●
●
●
●
●
●
●
●
●●
●
●
●●
●
●
●
●
●
●●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●●
●
● ●
●● ● ●● ● ●
●
●●●
●●
●●●
●●●
●
●●
●●
●●
●●
●
●●
●●
●●
●●
●
●
●●
● ●
●
●●
●
●●
●
●●
●●
●
●
●●●
●
●
●
●●
●●
●
●●
●●●
●
●●
●● ● ●●
●
●●
●●● ●
●●
●●
●●●●●
●
● ●
●●●
●
●●
● ●
● ●
●●●●
●
●
●●
●
●●
●
●●
●
●●
●
●●
●●
●
●●
●●
●●
●●
●●
●
●
●●
●●
●
●
●●
●
●●
●
●
●●●
●
●●
●
●●
●
●
●●
●
●
●
●●
●
●●
●
●●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●●
●
●
●●
●
●●
●
●
●
●
●
●
●
●●●
●
●
●
●
●●
●
●
●
●
●●
●●
●●
●
●
●
●●
●
●
●
●
●
●
●●●●●
● ●●● ● ●
●
● ●
●●
●●●
●●
●●
●
●●
●●
●●
●●
●
●●
●●
●
●●
●●
●
●●●
●
●
●●
●
●
●
●●
●●
●●
●●
●●
●●
●
●●●
●
●
●●
●
●
●●
●●
●●● ●● ●
●●
●●
●●●
●
●●
●●
●
●●
●●
●●
●●●
●
●
●●
●●●
●●
●
●
●●
●●
●●
●
●●
●
●●
●●
●●
●●
●●
●
●
●
●●
●
●●
●●
●
●●
●
●●
●
●●
●
●
●
●●
●●
●●
●● ●
● ●
●●
●●
●●
●●
●●
●●
●●
●● ●
●
●●●
●
●●
●●
●●
●●●
●
●
●
●●
●●
●●
●
●●
●●
●●
●
●●●
●
●●
●
●●
●●●
●● ●●●●
● ●
●● ●●● ●
●
●●
●●●
●●
●
●●
●
●●●
●●
●●
●
●●
●●●
●●
●
●●●
●
●
●●
●●●
●
●
●
●●
●●
●●
●●
●
●●
●
●●
●●
●
●●
●
●
●
●
●
●
●
●●
●●●
●
●●
●
●●●
● ●
●●
●●
●
●
●●
●
●●
●●
●●
●
●
●●
● ●●
●
●●●●● ●
●● ● ● ●
● ●●
●
●●●
●●●
●●
●
●●●
●●●
●●
●●
●●
●●●
●●
●
●●●●
●
●●●
●
●
●●
●
●●
●●
●●
●
●●
●
●●
●
●●
●●
●●●
●
●●
●
●
●●●
●
●●
●
●
●
●●
●●●●
●
●● ●● ●
●●●● ● ●
●●
●●●
●
●●●
●●
●
●●
●●
●●●
●
●
●●●
●●●
●
●●
●●
●
●●
●
●●
●●●
●
●●
●●●●
●
●●
●●
● ●
●●
●●
●●
●
●●
●●
●●
●
●
●●●●
●
●
●●
●
●●
●
●●
●
●●
●
●●
●●
●●
●●●
●●
●●
●●
●●
●●
●●
●●
●●
●
●●●
●●●
●
●●
●● ●
●●
●
●
●●
●●
●●
●
●●
●●
●●
●
●●●
●●
●●●
●
●
●●
●
●●●
●●
● ●
●
●
●
●●
●
●
●●●
●
●
●●
●●
●●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●
●●
●
●
●
●●
●
●
●
●●
●
●●
●
●●●● ● ●●
● ●●
●
●
●
● ●
●
●●
●
●●●
●●
●●
●●
●
●●
●
●●
●
●●
●●
●
●●●
●●●
●●
●●
●
●
●●
●●
●●
●
●●
●
●●
●●
●
●●
●
●●
●●●
●
●
●●
●
●
●●
●●
●
●
●●
●●●●
●●● ● ●●
● ● ● ●
●●●
● ●●
●
●●●
●●
●
●●
●●
●● ●●
●●
●●
●●
●
●●
●
●●
●
●●●
●●
●●
●
●
●●●●
●
●
●
●●
●●
●●
●● ●
●
●●
●●
●
●
●
●●●
●
●
●
●
●●●
●
●●
●
●
●
● ●
●
● ●●
●●●●●
●●●●●
●
●
●●
●● ●
●●●●
●●
● ●
●
●●●●
●●
● ● ●●●●
5000 ●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●●●
●●
●
●●
●
●
●●
●●
●
●●
●●
●
●
●●
●
●
●●
●●
●
●●
●
●
●●
●●●
●
●●●●
●●
●
●●●
●●
●●
●●●
●●
●●
●●
●●
●
●●
●●
●●●
●●
●
●
●●
●●
●●●
●●
●●
●●●●●
●●
●●
●●
●
●
●●
●●
●●●
●
●●
●●●
●●●●
●
●●
●●
●●
●●
●●
●
●●●
●
●●●●
●●
●●
●●
●● ●
●
●
●
●●
●
●●
●●●
●●
●
●●
●
● ●
●
●●
●●
●
●●
●
●●
●●●
●
●
●●
●
●●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●●
●
●●
●
●
●
●
●
●
●
●●
●
●●
●●
●
●●
●●
●●
●●
●
●
●
●
●●
●
●
●
●
●
●
●
●●
●●
●
●
●
●
●
●●
●
●
●●
●
●
●●
●●
●●
●●
●
●
●
●●
●
●
●
●●
●●●●
●
●●
● ●●
●●
●
●●●
●●
●●●●
●●
●●
● ●
●●
●●
●
●●
●
●●
●●
●
●● ●
●
●●●
●●●
●
●●
●●
●●
●●●
●●●
●
●●●
●●
●●●
●●
●●
●●
●
●●
●●●●
●
●●
●●
●●
●●●
●
●
●●
● ●
●
●●
●●
● ●
●●
●
●
●
●●
●●
●
●
●
●
●●
●
●●
●
●●
●●
●●
●●
●
●●● ●
●
●●
●
●●
●●
●●
●
●●
●
●●
●●
●
●●
●
●●
●● ●
●
●
●
●●
●●
●
●●
●●
●
●
●●●
●
●●
●●
●
●
●
●
●●
●●
●●●
●●
●
●●
●●
●●
●
●
●●
●●●
●●●
●
●
●
●●
●●
●●● ●●
●●
●
●●
●
●●
●
●●●
●
●●
●●
●●●
●●● ●●
●
●
●
●●
●●
●
●●●
●●●
●●
●
●
●●
●
●
●●
●
●●
●●●●●
●
●
●
●
●●●
●●
●
●●
●●
●
●●
●●
●●
●●●●
●
●●
●●
●
●●
●●●
●
●●
●
●●
●
●●●●
●●
●●
●
●●
●
●●
●●
●
●●
●
●
● ●
●
●●●
●●
●●
●●●●
●
●●
●●
●
●
●●
●
●●●
●
●
●●
●
●●
●
●
●●
●●
●●
● ●
●●
●
●●
●●
●●
●●
●
●
●●
●●●
●●
●●
●●
●●
●
●●
●●
●
●●
●●
●
●●
●●●
●
●●●
●●
● ●
●●
●
●●
●●
●
●
●●
●●
●
●●
●
●●
●●●
●
●●
●
●●
●●
●
●●
●
●
●●
●
●●
●●
●
●
●
●
●
●
●●
●
●●●
●
●●●
●
● ●
●●
● ●
●
●●
●
●
●●
●
●
●
●●
●
●●
●●
●
●●●
●●
●●
●
●●
●
●●
●
●
●●●
●●
●●
●
●●●
●● ●
●
●●
●
●
●●
●●
●●●
●
●●
●●● ●
●
●●
●●●
●
●
●●
●●
●
●
●●
●
●●
●●●
●
●
●●
●●
●●●
●●
●●● ●
●
●
●●
●
●
●●●●
●●●
●
●●
●
●
●●
●
●●
●●●
●●
●●
●●
●
●●
●●
●●
●
●●
●●
●●● ●
●●
●●
●●●
●
●●
●●
●●
●●●
●
●
●
●●
●
●●
●
●●
●
●
●●●●
●●
●
●●●
● ●
●●
●
●
●●
●
●
●
●●
●●
●●
●
●●
●●
●
●●●
●
●
●●
●●
●●
●●
●●●
●●
●●
● ●
●
●●
●●
●●
●●
●
●
●●
●
●●●
●● ●
●●
●
●●
●●
●
●
●●
●● ●
●●
●
●
●●
●
●
●●
●
●●
●
●●
●
●
●
●
●●
●
●●
●
●
●●● ●
● ●
●● ●●
●●●
●
●
●●●
●
●
●●
●●
●
●
●● ●
●●
●●
●
●●
●
●●
●●
●●●●●●
●●
●●●
●
●●
●
●●
●●
●●
●●
●
●●●
●●
●●●
●
●●
●
●
●●●
●●●●
●●
●
●●
●●
●●
●●
●●
●●
●
●
●●
●
●●
●
●●
●●
●
● ●●●
●● ●
●
●
●
●●
●
●●
●
●●
●●
●
●●●
●●
●●
●
●
●●●
●●
●●●
●●
●●
●
●●
●
●
●●●
●●●
●●
●●
●●
●●
●●
●
●
●●
●●
●
●●●●
●●
●●
●
●●
●●
●
●●
●
●●
●
●●
●●●
●
●
●●●
●●
●●
●
●
●●
●
●●
●●
●
●●●
●
●
●●
●
●●
●●
●●
●
●●
●●●
●●
●●
● ●●●
●●
●●
●●
●●●
●●
●●
●●
●
●
●●●●
●●
●●
●●
●●
●
●
●●
●
●●●
●●
●
●
●●
●●●
●●
●
●●
●●
●
●●●
●
●
●●●
●
●●●
●
●
●
●
●●
●
●
●●
●
●●
●●
●
●
●
●●●
●
●●
● ●
●●●
●●
●
●
●●
●
●●
●● ●●
●●●
●●
●●
●●
●
●
●
●●
●●
●●
● ●
●
●●
● ●
●
●● ●●
●●
●
●
●●
●
●●
●●
●●
●●
●●
●●
●
●●
●●
●●
●●
●●
●●
●●
●
●●
●
●●
●●
●●
●● ●
●●
●●
●●
●● ●
●
●●
●●
●●●
●
●
●●
●●
●●
●●
●●
●●
●
● ●●●
●●
●
●●
●●
●
●
●●
●
●
●●
●
●
●●
●●●
●
●●●
●
●●
●●
●
●●
●●
●
●●
●
●●
●●
●
●●
●●
●●
●●
●●●
●
●
●●
●●
●●●●
●●
●
●
●●
●●
●
●
●●
●●●
●●
●●
●●
●●
●●●
●●
●●
●●
●●
●●
●
●●
●
●●
●●
●
●●
●●
●●
●●
●●
●●●
●●
●
●
●●
●●
●
●
●
●●
●
●
●
●●
●
●
●
●
●
●●
●
●
●
●●
●●
●●
●●● ●
●
●
●
●
●
●
●●
●
●
●
●● ●
●●
●●
●●
●●
●●
●
●●
● ●
●
●
●●
●●
●●
●●
●●
●● ●
●●
●
●
●●
●●
●
●●●
●●
●
●●
●
●●
●●
●●
●
● ●●
●●
●
●
●
●●
●●
●●
●●
●
●
●●
●
●●
●
●●●
●●
●
●
●●●
0
0 1 2 3 4 5
carat
Data Visualization with ggplot2
Diamonds
> ggplot(diamonds, aes(carat, price)) +
geom_point(alpha = 0.1)
15000
price
10000
5000
0
0 1 2 3 4 5
carat
Data Visualization with ggplot2
Diamonds
> ggplot(diamonds, aes(carat, price)) +
geom_point(alpha = 0.01, size = 4)
15000
price
10000
5000
0
0 1 2 3 4 5
carat
Data Visualization with ggplot2
Diamonds
> ggplot(diamonds, aes(carat, price)) +
geom_point(alpha = 0.3, shape = ".")
15000
price
10000
5000
0
0 1 2 3 4 5
carat
DATA VISUALIZATION WITH GGPLOT2
Let’s practice!
DATA VISUALIZATION WITH GGPLOT2
Bar Plots
Data Visualization with ggplot2
Histogram
> ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram()
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to
adjust this.
> diff(range(iris$Sepal.Width)) / 30
[1] 0.08
20
count
Slightly different binning algorithm 10
2 3 4
Sepal.Width
Data Visualization with ggplot2
Histogram
> ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram(binwidth = 0.1)
count
x axis labels between the bars
10
Histogram
> ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram( aes(y = ..density..), binwidth = 0.1)
internal data frame
1.5
1.0
density
y axis re-labeled
0.5
0.0
Different Species
> ggplot(iris, aes(x = Sepal.Width , fill = Species)) +
geom_histogram(binwidth = 0.1)
20
Species
setosa
count
versicolor
10 virginica
Different Species
> ggplot(iris, aes(x = Sepal.Width, fill = Species)) +
geom_histogram(binwidth = 0.1, position = "stack")
20
Species
setosa
count
versicolor
10 virginica
dodge
> ggplot(iris, aes(x = Sepal.Width, fill = Species)) +
geom_histogram(binwidth = 0.1, position = "dodge")
12.5
10.0
7.5 Species
setosa
count
versicolor
5.0
virginica
2.5
0.0
fill
> ggplot(iris, aes(x = Sepal.Width, fill = Species)) +
geom_histogram(binwidth = 0.1, position = "fill")
1.00
0.75
Species
setosa
count
0.50
versicolor
virginica
0.25
0.00
Bar Plot
● geom_bar()
● All positions from before available
● Two types
● Absolute counts
● Distributions
Data Visualization with ggplot2
Habits of Mammals 30
count
> ggplot(sleep, aes(vore)) + geom_bar()
10
> str(sleep)
'data.frame': 76 obs. of 3 variables:
$ vore : Factor w/ 4 levels "Carnivore","Herbivore",..
$ total: num 12.1 17 14.4 14.9 4 14.4 8.7 10.1 3 5.3 ... 0
$ rem : num NA 1.8 2.4 2.3 0.7 2.2 1.4 2.9 NA 0.6 ... Carnivore Herbivore Insectivore Omnivore
vore
Data Visualization with ggplot2
Habits of Mammals 30
count
> ggplot(sleep, aes(vore)) + geom_bar(stat = "bin")
10
> str(sleep)
'data.frame': 76 obs. of 3 variables:
$ vore : Factor w/ 4 levels "Carnivore","Herbivore",..
$ total: num 12.1 17 14.4 14.9 4 14.4 8.7 10.1 3 5.3 ... 0
$ rem : num NA 1.8 2.4 2.3 0.7 2.2 1.4 2.9 NA 0.6 ... Carnivore Herbivore Insectivore Omnivore
vore
Data Visualization with ggplot2
> str(iris_summ)
'data.frame': 3 obs. of 3 variables:
$ Species: Factor w/ 3 levels "setosa","versicolor",..: 1 2 3
$ avg : num 3.43 2.77 2.97
$ stdev : num 0.379 0.314 0.322
Data Visualization with ggplot2
First try
> ggplot(iris_summ, aes(x = Species, y = avg)) +
geom_bar()
Error : Mapping a variable to y and also using stat="bin".
With stat="bin", it will attempt to set the y value to the count of cases in each group.
This can result in unexpected behavior and will not be allowed in a future version of ggplot2.
If you want y to represent counts of cases, use stat="bin" and don't map a variable to y.
If you want y to represent values in the data, use stat="identity".
See ?geom_bar for examples. (Defunct; last used in version 0.9.2)
avg
identity
> ggplot(iris_summ, aes(x = Species, y = avg)) +
geom_bar(stat = "identity")
2
avg
geom_errorbar
> ggplot(iris_summ, aes(x = Species, y = avg)) +
geom_bar(stat = "identity", fill = "grey50") +
geom_errorbar(aes(ymin = avg - stdev, ymax = avg + stdev),
width = 0.2)
3
Dynamite Plot
avg
Let’s practice!
DATA VISUALIZATION WITH GGPLOT2
Line Plots
Time Series
Data Visualization with ggplot2
Beaver
> str(beaver)
'data.frame': 101 obs. of 3 variables:
$ time : POSIXct, format: "2000-01-01 09:30:00" "2000-01-01 ...
$ temp : num 36.6 36.7 36.9 37.1 37.2 ...
$ active: Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
Data Visualization with ggplot2
Beaver
> ggplot(beaver, aes(x = time, y = temp)) +
geom_line()
38.0
temp
37.5
37.0
36.5
09:00 12:00 15:00 18:00 21:00 00:00
time
Data Visualization with ggplot2
Beaver
> ggplot(beaver, aes(x = time, y = temp, col = factor(active))) +
geom_line()
38.0
factor(active)
temp
37.5 0
1
37.0
36.5
09:00 12:00 15:00 18:00 21:00 00:00
time
Data Visualization with ggplot2
Shape
Hatching Outlines Labels
linetype
> ggplot(fish, aes(x = Year, y = Capture, linetype = Species)) +
geom_line()
> names(fish)
[1] "Species" "Year" "Capture"
6e+05
Species
Pink
4e+05
Chum
Capture
Sockeye
Coho
Rainbow
2e+05
Chinook
Atlantic
0e+00
size
> ggplot(fish, aes(x = Year, y = Capture, size = Species)) +
geom_line()
6e+05
Species
Pink
4e+05
Chum
Capture
Sockeye
Coho
Rainbow
2e+05
Chinook
Atlantic
0e+00
color
> ggplot(fish, aes(x = Year, y = Capture, color = Species)) +
geom_line()
6e+05
Species
Pink
4e+05
Chum
Capture
Sockeye
Coho
Rainbow
2e+05
Chinook
Atlantic
0e+00
fill
> ggplot(fish, aes(x = Year, y = Capture, fill = Species)) +
geom_area()
1200000
900000 Species
Pink
Chum
Capture
Sockeye
600000
Coho
Rainbow
Chinook
300000
Atlantic
fill (2)
> ggplot(fish, aes(x = Year, y = Capture, fill = Species)) +
geom_area(position = "fill")
1.00
Species
0.75
Pink
Chum
Capture
Sockeye
0.50
Coho
Rainbow
Chinook
0.25 Atlantic
0.00
geom_ribbon
> ggplot(fish, aes(x = Year, y = Capture, fill = Species)) +
geom_ribbon(aes(ymax = Capture, ymin = 0), alpha = 0.3)
6e+05
Species
Pink
4e+05
Chum
Capture
Sockeye
Coho
Rainbow
2e+05
Chinook
Atlantic
0e+00
Let’s practice!