Introduction to
ggplot2
package
Felipe Dalla Lana, Ph. D.
2
Hadley Wickham
• Chief Scientist at Rstudio
• University of Auckland
• Stanford University
• Rice University
First released - July of 2007
40 version since
Authors of current version
(3.3.3)
Hadley Wickham
Winston Chang
Lionel Henry
Thomas Lin Pedersen
Kohske Takahashi
Claus Wilke
Kara Woo
Hiroaki Yutani
Dewey Dunnington
What is ggplot2
April 2006
v. 0.2.2
October 2008
v. 0.4.2
3
How about ggplot1?
The package grammar
ggplot function and
data
Geometric elements
Facets, scale, labs,
coordinates
Theme
Save
Aesthetic mappings
4
Data and ggplot function
5
Data and ggplot function
6
7
Geometric elements
8
Geometric elements
9
Geometric elements
Geometric elements
10
geom_violin() geom_histogram() geom_density()
11
Geometric elements
geom_point()
geom_point()+
geom_smooth()+
geom_rug()geom_dotplot()
12
Geometric elements
13
Geometric elements –
extensions packages
plotROC package
geom_roc()
ggbump package
geom_bump()
gggenes package
geom_gene_arrow()
Aesthetics
• How the variables will be mapped
• Inside of aes() command
• If aes() is in ggplot() function, the aesthetics will be the same for all geom_xxx()
• If aes() is in a geom_xxx(), the aesthetics will be especificaly for that geom_xxx()
Most common
• X and y-axis
• Color; fill; and alpha
• Shape
• Size
• Linetype
14
Aesthetics
15
Aesthetics - color
16
Aesthetics - Fill
17
Aesthetics – Alpha and size
18
Aesthetics - shape
19
Aesthetics
20
Aesthetics
21
Scales
Most common
• scale_*_continuous()
• scale_*_discrete()
• scale_*_manual()
• scale_*_date()
• scale_*_reverse()
• scale_*_log10()
• scale_*_gradient2()
→Control aesthetics mapping
22
Scales
#RRGGBBAA
23
Scales
colorbrewer2.org
24
Scales
ggsci package
inauguration package
25
Scales
@CedScherer
26
Facets
What if we want the same plot
with severity side-by-side?
Prepare data
Prepare a base plot
27
Facets
28
Facets
29
Labels
30
Themes
31
Themes
ggthemes package
32
Themes - customization
The options are countless
33
Themes
- customization
34
Themes
- customization
35
Save
36
Save
37
Save
38
Extra
39
40
Extra
41
The package grammar
DATA (survey_data_long) %>%
ggplot function –
Geometric elements (geom_boxplot and geom_jitter)
Aesthetic (x, y, and color)
Scale(y-continues and color [ggsci])
Facets (facet_wrap)
Themes(customized)
Save(ggsave – tiff, 85x140 mm)
Get inspired
Del Ponte, E. M., & Belachew, K.
https://doi.org/10.17605/OSF.IO/XEJAZ
Belachew, K., Senbeta, G.A., Garedew, W. et al. Altitude is
the main driver of coffee leaf rust epidemics: a large-scale
survey in Ethiopia. Trop. plant pathol. 45, 511–521 (2020).
https://doi.org/10.1007/s40858-020-00383-4
43
44
45
@florencelydia11
@ijeamaka_a
46