Post on 03-Apr-2018
7/29/2019 Bauer Vortrag Graphics
1/49
IntroductionThe grammar of graphics
LayersDemo
ggplot2
Elegant graphics for Data Analysis
Johannes BauerSeminar (mit Bachelorarbeit)
20.1.2011
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
http://find/7/29/2019 Bauer Vortrag Graphics
2/49
IntroductionThe grammar of graphics
LayersDemo
Outline
1 Introduction
2 The grammar of graphics
The grammar of graphicsAesthetic attributesGeometric objectsFaceting
3 Layers
4 Demo
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
http://find/7/29/2019 Bauer Vortrag Graphics
3/49
IntroductionThe grammar of graphics
LayersDemo
Introduction
ggplot2 is a plotting system for R
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
http://find/7/29/2019 Bauer Vortrag Graphics
4/49
IntroductionThe grammar of graphics
LayersDemo
Introduction
ggplot2 is a plotting system for Rdeveloped by Hadley Wickham
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
I d i
http://find/7/29/2019 Bauer Vortrag Graphics
5/49
IntroductionThe grammar of graphics
LayersDemo
Introduction
ggplot2 is a plotting system for Rdeveloped by Hadley Wickham
based on the grammar of graphics(Wilkenson, 2005)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
I t d ti
http://find/7/29/2019 Bauer Vortrag Graphics
6/49
IntroductionThe grammar of graphics
LayersDemo
Introduction
ggplot2 is a plotting system for Rdeveloped by Hadley Wickham
based on the grammar of graphics(Wilkenson, 2005)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
Introd ction
http://find/7/29/2019 Bauer Vortrag Graphics
7/49
IntroductionThe grammar of graphics
LayersDemo
Installation
install.packages(ggplot2)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
Introduction The grammar of graphics
http://find/7/29/2019 Bauer Vortrag Graphics
8/49
IntroductionThe grammar of graphics
LayersDemo
The grammar of graphicsAesthetic attributesGeometric objectsFaceting
The grammar of graphics
The grammar tells us that a statistical graphic is a mapping fromdata to aesthetic attributes of geometric objects.
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
Introduction The grammar of graphics
http://find/7/29/2019 Bauer Vortrag Graphics
9/49
IntroductionThe grammar of graphics
LayersDemo
The grammar of graphicsAesthetic attributesGeometric objectsFaceting
The grammar of graphics
aesthetic attributes aesthetics
colour
shapesize
geometric objects geoms
points
lines
bars
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
Introduction The grammar of graphics
http://find/7/29/2019 Bauer Vortrag Graphics
10/49
IntroductionThe grammar of graphics
LayersDemo
The grammar of graphicsAesthetic attributesGeometric objectsFaceting
aesthetic attributes
example: qplot(carat, price, data=dsmall, colour=color)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
Introduction The grammar of graphics
http://find/7/29/2019 Bauer Vortrag Graphics
11/49
IntroductionThe grammar of graphics
LayersDemo
The grammar of graphicsAesthetic attributesGeometric objectsFaceting
aesthetic attributes
example: qplot(carat, price, data=dsmall, colour=color)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
Introduction The grammar of graphics
http://find/http://goback/7/29/2019 Bauer Vortrag Graphics
12/49
The grammar of graphicsLayersDemo
g g pAesthetic attributesGeometric objectsFaceting
aesthetic attributes
example: qplot(carat, price, data=dsmall, shape=cut)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
Introduction The grammar of graphics
http://find/7/29/2019 Bauer Vortrag Graphics
13/49
The grammar of graphicsLayersDemo
g gAesthetic attributesGeometric objectsFaceting
aesthetic attributes
example: qplot(carat, price, data=dsmall, size=x*y*z)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
Introduction The grammar of graphics
http://find/http://goback/7/29/2019 Bauer Vortrag Graphics
14/49
The grammar of graphicsLayersDemo
Aesthetic attributesGeometric objectsFaceting
other aesthetic attributes
example: qplot(carat, price, data=diamonds)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionTh f hi
The grammar of graphicsA h i ib
http://find/http://goback/7/29/2019 Bauer Vortrag Graphics
15/49
The grammar of graphicsLayersDemo
Aesthetic attributesGeometric objectsFaceting
other aesthetic attributes
example: qplot(carat, price, data=diamonds)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionTh f hi
The grammar of graphicsA th ti tt ib t
http://find/http://goback/7/29/2019 Bauer Vortrag Graphics
16/49
The grammar of graphicsLayersDemo
Aesthetic attributesGeometric objectsFaceting
other aesthetic attributes
example: qplot(carat, price, data=diamonds, alpha=I(1/20))
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
The grammar of graphicsAesthetic attributes
http://find/7/29/2019 Bauer Vortrag Graphics
17/49
The grammar of graphicsLayersDemo
Aesthetic attributesGeometric objectsFaceting
other aesthetic attributes
example: qplot(carat, price, data=diamonds, alpha=I(1/20))
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
The grammar of graphicsAesthetic attributes
http://find/7/29/2019 Bauer Vortrag Graphics
18/49
The grammar of graphicsLayersDemo
Aesthetic attributesGeometric objectsFaceting
geometric objects
Geometric objects describe the type of object that is used todisplay the data.
some geoms
points (default)
smooth
bars
histogram
density
...
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
The grammar of graphicsAesthetic attributes
http://find/http://goback/7/29/2019 Bauer Vortrag Graphics
19/49
The grammar of graphicsLayersDemo
Aesthetic attributesGeometric objectsFaceting
Adding a smoother to a scatterplot
example: qplot(carat, price, data=diamonds, geom=smooth)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
The grammar of graphicsAesthetic attributes
http://find/7/29/2019 Bauer Vortrag Graphics
20/49
The grammar of graphicsLayersDemo
Aesthetic attributesGeometric objectsFaceting
Adding a smoother to a scatterplot
example: qplot(carat, price, data=diamonds, geom=smooth)
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
The grammar of graphicsAesthetic attributes
http://find/7/29/2019 Bauer Vortrag Graphics
21/49
g g pLayersDemo
Geometric objectsFaceting
Adding a smoother to a scatterplot
example:qplot(carat, price, data=diamonds, geom=c(points, smooth))
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
The grammar of graphicsAesthetic attributes
http://find/7/29/2019 Bauer Vortrag Graphics
22/49
g g pLayersDemo
Geometric objectsFaceting
Adding a smoother to a scatterplot
example:qplot(carat, price, data=diamonds, geom=c(points, smooth))
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
The grammar of graphicsAesthetic attributes
http://find/7/29/2019 Bauer Vortrag Graphics
23/49
LayersDemo
Geometric objectsFaceting
Adding a smoother to a scatterplot
Methods
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
The grammar of graphicsAesthetic attributes
http://find/7/29/2019 Bauer Vortrag Graphics
24/49
LayersDemo
Geometric objectsFaceting
Adding a smoother to a scatterplot
Methods
method = loess (default)uses a smooth local regression
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
L
The grammar of graphicsAesthetic attributesG i bj
http://find/7/29/2019 Bauer Vortrag Graphics
25/49
LayersDemo
Geometric objectsFaceting
Adding a smoother to a scatterplot
Methods
method = loess (default)uses a smooth local regression
method = lmfits a linear model
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
L
The grammar of graphicsAesthetic attributesG t i bj t
http://find/7/29/2019 Bauer Vortrag Graphics
26/49
LayersDemo
Geometric objectsFaceting
Adding a smoother to a scatterplot
Methods
method = loess (default)uses a smooth local regression
method = lmfits a linear model
method = rlmworks like lm but uses a robust fitting algorithm so that
outliers dont affect the fit as much.
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
Layers
The grammar of graphicsAesthetic attributesGeometric objects
http://find/7/29/2019 Bauer Vortrag Graphics
27/49
LayersDemo
Geometric objectsFaceting
Adding a smoother to a scatterplot
Methods
method = loess (default)uses a smooth local regression
method = lmfits a linear model
method = rlmworks like lm but uses a robust fitting algorithm so that
outliers dont affect the fit as much....
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
Layers
The grammar of graphicsAesthetic attributesGeometric objects
http://find/7/29/2019 Bauer Vortrag Graphics
28/49
LayersDemo
Geometric objectsFaceting
Faceting
With aesthetics we can compare different subgroups of ourdataframe on the same plot.
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
Layers
The grammar of graphicsAesthetic attributesGeometric objects
http://find/7/29/2019 Bauer Vortrag Graphics
29/49
LayersDemo
Geometric objectsFaceting
Faceting
With aesthetics we can compare different subgroups of ourdataframe on the same plot.
Faceting also splits the data in subgroups but displays it onmultiple plots.
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
Layers
The grammar of graphicsAesthetic attributesGeometric objects
http://find/7/29/2019 Bauer Vortrag Graphics
30/49
LayersDemo
Geometric objectsFaceting
Faceting
qplot(carat, data=diamonds, facets = color . ,geom = histogram, binwidth = 0.1, xlim=c(0,3))
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
Layers
The grammar of graphicsAesthetic attributesGeometric objects
http://find/http://goback/7/29/2019 Bauer Vortrag Graphics
31/49
yDemo
jFaceting
Faceting
qplot(carat, data=diamonds, facets = color . ,geom = histogram, binwidth = 0.1, xlim=c(0,3))
Methodsfacets = color .
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
Layers
The grammar of graphicsAesthetic attributesGeometric objects
http://find/7/29/2019 Bauer Vortrag Graphics
32/49
yDemo
jFaceting
Faceting
qplot(carat, data=diamonds, facets = color . ,geom = histogram, binwidth = 0.1, xlim=c(0,3))
Methodsfacets = color .
geom = histogram has options like the binwidth
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
Layers
The grammar of graphicsAesthetic attributesGeometric objects
http://find/7/29/2019 Bauer Vortrag Graphics
33/49
Demo Faceting
Faceting
qplot(carat, data=diamonds, facets = color . ,geom = histogram, binwidth = 0.1, xlim=c(0,3))
Methodsfacets = color .
geom = histogram has options like the binwidth
xlim=c(0,3) sets the limits of the x-axes
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphics
Layers
The grammar of graphicsAesthetic attributesGeometric objects
http://find/7/29/2019 Bauer Vortrag Graphics
34/49
Demo Faceting
Faceting
qplot(carat, data=diamonds, facets = color . ,geom = histogram, binwidth = 0.1, xlim=c(0,3))
Methods
facets = color .
geom = histogram has options like the binwidth
xlim=c(0,3) sets the limits of the x-axes
attributes xlim, ylim, main, xlab, ylab are equivalent to basicplot
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphicsLayersD
The grammar of graphicsAesthetic attributesGeometric objectsF ti
http://find/7/29/2019 Bauer Vortrag Graphics
35/49
Demo Faceting
Faceting
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphicsLayersDemo
The grammar of graphicsAesthetic attributesGeometric objectsFaceting
http://find/7/29/2019 Bauer Vortrag Graphics
36/49
Demo Faceting
Differences from the basic plot function
qplot() is not generic.We cannot use points(), lines() or text() to add furthergraphic elements. We need additional layers.
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphicsLayersDemo
http://find/7/29/2019 Bauer Vortrag Graphics
37/49
Demo
creating a plot
When we used qplot(), it did a lot of things for us:
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphicsLayersDemo
http://find/7/29/2019 Bauer Vortrag Graphics
38/49
Demo
creating a plot
When we used qplot(), it did a lot of things for us:
it created a plot object
it added layers
displays the result
To create the plot ourself, we use ggplot().
ggplot() has two arguments
the dataframeaesthetic mapping
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphicsLayersDemo
http://find/7/29/2019 Bauer Vortrag Graphics
39/49
Demo
creating a plot - example
Creates a plot but there is nothing to see.
p
7/29/2019 Bauer Vortrag Graphics
40/49
Demo
creating a plot - example
Creates a plot but there is nothing to see.
p
7/29/2019 Bauer Vortrag Graphics
41/49
creating a plot - example
Creates a plot but there is nothing to see.
p
7/29/2019 Bauer Vortrag Graphics
42/49
creating a plot - complex example
p
7/29/2019 Bauer Vortrag Graphics
43/49
creating a plot - complex example
p
7/29/2019 Bauer Vortrag Graphics
44/49
creating a plot - simplified complex example
p
7/29/2019 Bauer Vortrag Graphics
45/49
creating a plot - simplified complex example
p
7/29/2019 Bauer Vortrag Graphics
46/49
creating a plot - simplified complex example
p
7/29/2019 Bauer Vortrag Graphics
47/49
more examples in R
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphicsLayersDemo
http://find/7/29/2019 Bauer Vortrag Graphics
48/49
References:
ggplot2 - Elegant Graphics for Data Analysis
Hadley Wickham, Springer 2009Online Referencehttp://had.co.nz/ggplot2
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
IntroductionThe grammar of graphicsLayersDemo
http://find/7/29/2019 Bauer Vortrag Graphics
49/49
Thank you.Questions?
Johannes Bauer ggplot2 Elegant graphics for Data Analysis
http://find/