Documente Academic
Documente Profesional
Documente Cultură
Coordinates
Data Visualization with ggplot2
Coordinates Layer
Controls plot dimensions
coord_
coord_cartesian()
Data Visualization with ggplot2
Zooming in
scale_x_continuous(limits = ...)
xlim()
coord_cartesian(xlim = ...)
Data Visualization with ggplot2
Original Plot
> iris.smooth <- ggplot(iris, aes(x = Sepal.Length,
y = Sepal.Width,
col = Species)) +
geom_point(alpha = 0.7) + geom_smooth()
> iris.smooth
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
scale_x_continous
> iris.smooth + scale_x_continuous(limits = c(4.5, 5.5))
geom_smooth: method="auto" and size of largest group is <1000, so
using loess. Use 'method = x' to change the smoothing method.
Warning messages:
1: Removed 7 rows containing missing values (stat_smooth).
2: Removed 39 rows containing missing values (stat_smooth).
3: Removed 49 rows containing missing values (stat_smooth).
4: Removed 95 rows containing missing values (geom_point).
Data Visualization with ggplot2
scale_x_continous
> iris.smooth + scale_x_continuous(limits = c(4.5, 5.5))
4.5
4.5
4.0
4.0
Sepal.Width
Sepal.Width
3.5 Species 3.5 Species
setosa setosa
versicolor versicolor
3.0 3.0
virginica virginica
2.5 2.5
2.0 2.0
xlim
> iris.smooth + xlim(c(4.5, 5.5))
Warning messages: ...
4.5
4.5
4.0
4.0
Sepal.Width
Sepal.Width
3.5 Species 3.5 Species
setosa setosa
versicolor versicolor
3.0 3.0
virginica virginica
2.5 2.5
2.0 2.0
coord_cartesian
> iris.smooth + coord_cartesian(xlim = c(4.5, 5.5))
4.5 4.5
4.0 4.0
Sepal.Width
Sepal.Width
3.5 Species 3.5 Species
setosa setosa
versicolor versicolor
3.0 3.0
virginica virginica
2.5 2.5
2.0 2.0
Aspect Ratio
Height-to-width ratio
Deception!
Standardization a!empts
Typically 1:1
Data Visualization with ggplot2
Sunspots
> library(reshape2); library(zoo)
> sunspots.m <- data.frame(year = index(sunspot.month),
value = melt(sunspot.month)$value)
> ggplot(sunspots.m, aes(x = year, y = value)) +
geom_line() +
coord_equal() # a 1:1 aspect ratio
150
value
100
50
Sunspots
> ggplot(sunspots.m, aes(x = year, y = value)) +
geom_line() +
coord_fixed(0.055)
250
value
200
150
100
50
0
1800 1900 2000
year
DATA VISUALIZATION WITH GGPLOT2
Lets practice!
DATA VISUALIZATION WITH GGPLOT2
Facets
Data Visualization with ggplot2
Facets
Straight-forward yet useful
Concept of Small Multiples
Edward Tu!e
Visualization of Quantitative Information, 1983
Data Visualization with ggplot2
Facets
15
10
0
0 5 10 15 20
Data Visualization with ggplot2
Facets
15 15 15 15
10 10 10 10
5 5 5 5
0 0 0 0
0 5 10 15 20 0 5 10 15 20 0 5 10 15 20 0 5 10 15 20
Data Visualization with ggplot2
Facets
15 15 15 15
10 10 10 10
5 5 5 5
0 0 0 0
0 5 10 15 20 0 5 10 15 20 0 5 10 15 20 0 5 10 15 20
Data Visualization with ggplot2
iris.wide
> p <- ggplot(iris.wide, aes(x = Length, y = Width, col = Part)) +
geom_point(position = position_jitter(), alpha = 0.7) +
scale_color_brewer(palette = "Set1") +
coord_fixed()
> p
3
Part
Width
Petal
2 Sepal
0
2 4 6 8
Length
Data Visualization with ggplot2
Petal
2
Sepal
1
0
2 4 6 8 2 4 6 8 2 4 6 8
Length
Data Visualization with ggplot2
6 8
6
4
6
versicolor
Measure Measure Measure
virginica
setosa
Length Length Length
4
Width
Width Width
4
2
2
2
0
Petal Sepal Petal Sepal Petal Sepal
Part Part Part
Data Visualization with ggplot2
iris.tidy
> ggplot(iris.tidy, aes(x = Measure, y = Value, col = Part)) +
geom_jitter() +
facet_grid(. ~ Species)
4
Petal
Sepal
2
0
Length Width Length Width Length Width
Measure
Data Visualization with ggplot2
iris.tidy - wrong
8
setosa
4
geom_jitter() +
facet_grid(Species ~ .)
0
8
6
Part
versicolor
Value
Petal
4
Sepal
2
0
8
6
virginica
4
2
0
Length Width
Measure
Data Visualization with ggplot2
Other options
Split according to rows and columns
Wrap subplots into columns
DATA VISUALIZATION WITH GGPLOT2
Lets practice!