Sunteți pe pagina 1din 24

DATA VISUALIZATION WITH GGPLOT2

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

5 6 7 8 4.50 4.75 5.00 5.25 5.50


Sepal.Length Sepal.Length

Original zoomed in, scale_x_continous()


Parts of original data set filtered!
Data Visualization with ggplot2

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

5 6 7 8 4.50 4.75 5.00 5.25 5.50


Sepal.Length Sepal.Length

Original zoomed in, xlim()


Same eect as scale_x_continuous()
Data Visualization with ggplot2

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

5 6 7 8 4.50 4.75 5.00 5.25 5.50


Sepal.Length Sepal.Length

Original zoomed in, xlim()


just zoom in
Data Visualization with ggplot2

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

250 1. Oscillating period of 11 years


2. Sunspot numbers change over long periods
200

150
value

100

50

1800 1900 2000


year
Data Visualization with ggplot2

Sunspots
> ggplot(sunspots.m, aes(x = year, y = value)) +
geom_line() +
coord_fixed(0.055)

1. Oscillating period of 11 years


2. Sunspot numbers change over long periods
3. Sunspots arise more quickly than they appear

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

iris.wide Add another variable


Aid in visual perception

> 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 + facet_grid(. ~ Species) rows ~ columns

setosa versicolor virginica


4
Part
3
Width

Petal
2
Sepal
1
0
2 4 6 8 2 4 6 8 2 4 6 8
Length
Data Visualization with ggplot2

iris.wide2 Each plot has separate y axis


Three dierent plot functions

> ggplot(iris.wide2, aes(x = Part, y = setosa, col = Measure)) +


geom_jitter()
> ggplot(iris.wide2, aes(x = Part, y = versicolor, col = Measure)) +
geom_jitter()
> ggplot(iris.wide2, aes(x = Part, y = virginica, col = Measure)) +
geom_jitter()

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)

setosa versicolor virginica


8














6
















Part



Value




4






Petal












Sepal





















2











































0
Length Width Length Width Length Width
Measure
Data Visualization with ggplot2

iris.tidy - wrong
8

setosa



4






> ggplot(iris.tidy, aes(x = Measure, y = Value, col = Part)) + 2


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!

S-ar putea să vă placă și