-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathggplot2_Grammar_Adv-Layers.Rmd
363 lines (247 loc) · 7.67 KB
/
ggplot2_Grammar_Adv-Layers.Rmd
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
---
title: "ggplot2 & Grammar of Graphics<br><br>The Four Advanced Layers"
subtitle: "Facets, Statistical Transformations,<br>Coordinate Systems, Themes"
author: "StatistikinDD"
date: "Created: `r Sys.Date()`"
output:
xaringan::moon_reader:
chakra: libs/remark-latest.min.js
lib_dir: libs
css: ["libs/_css/xaringan-themer.css", "libs/_css/my_css.css"]
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
slideNumberFormat: "%current%"
ratio: 16:9
---
```{r setup, include = FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(echo = FALSE, warning = FALSE, message = FALSE, comment = "")
library(tidyverse)
library(ggthemes)
# From https://cran.r-project.org/web/packages/xaringanthemer/vignettes/xaringanthemer.html
library(xaringanthemer)
style_mono_accent(
base_color = "#1c5253",
header_font_google = google_font("Josefin Sans"),
text_font_google = google_font("Montserrat", "300", "300i"),
code_font_google = google_font("Fira Mono"),
outfile = "libs/_css/xaringan-themer.css"
)
theme_set(theme_gray(base_size = 18))
```
# ggplot2: Layers
.pull-left[
Next to the necessary basic layers, ...
.content-box-grey[
**1. Data**
**2. Aesthetics**
**3. Geometries**
]
... there are four advanced / optional layers:
.content-box-gray[
**4. Facets: Display subgroups in separate plotting areas**
**5. Statistical transformations**
**6. Coordinate systems**
**7. Themes: *Non data ink* **
]
]
---
# Our Starting Point
## A Basic Scatterplot
.pull-left[
```{r more-aes, echo = TRUE, eval = FALSE}
ggplot(diamonds, aes(x = carat, y = price)) + #<<
geom_point(size = 0.5, alpha = 0.5) + #<<
labs(title = "Diamonds: Price by Carat (weight)",
x = "Carat", y = "Price",
caption = "Diamonds dataset, ggplot2")
```
* To display over 50.000 data points, we use both a reduced point size and some opacity
* Note that both price and carat are heavily right-skewed
]
.pull-right[
```{r more-aes-exec, echo = FALSE, eval = TRUE, ref.label = "more-aes", cache = TRUE}
```
]
---
# Facets (Layer 4)
.pull-left[
```{r facets, echo = TRUE, eval = FALSE}
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(size = 0.5, alpha = 0.5) +
facet_wrap(vars(cut)) + #<<
labs(title = "Diamonds: Price by carat (weight)",
x = "Carat", y = "Price",
caption = "Diamonds dataset, ggplot2")
```
* Specify layout using the
**nrow** and **ncol** parameters
* You can use the formula interface:
**facet_wrap(~ cut)**
* You can facet by two variables
* Alternative to **facet_wrap()**: **facet_grid()**
]
.pull-right[
```{r facets-exec, echo = FALSE, eval = TRUE, ref.label = "facets", cache = TRUE}
```
]
---
# Statistical transformations (Layer 5)
## Adding a Linear Smooth (lm)
.pull-left[
```{r stats-lm, echo = TRUE, eval = FALSE}
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(size = 0.5, alpha = 0.5) +
geom_smooth(method = "lm") + #<<
labs(title = "Diamonds: Price by Carat (weight)",
subtitle = "Smoothing Method: lm", #<<
x = "Carat", y = "Price",
caption = "Diamonds dataset, ggplot2")
```
* Check out the **stat_xxx()** functions
* Use case: *stat_summary()* to show means in boxplots
]
.pull-right[
```{r stats-lm-exec, echo = FALSE, eval = TRUE, ref.label = "stats-lm", cache = TRUE}
```
]
---
# Statistical transformations (Layer 5)
## Adding a Nonlinear Smooth (gam)
```{r, echo = FALSE}
theme_set(theme_gray(base_size = 18))
```
.pull-left[
```{r stats-gam, echo = TRUE, eval = FALSE}
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(size = 0.5, alpha = 0.5) +
geom_smooth(method = "gam") + #<<
labs(title = "Diamonds: Price by Carat (weight)",
subtitle = "Smoothing Method: gam", #<<
x = "Carat", y = "Price",
caption = "Diamonds dataset, ggplot2")
```
GAM = Generalized Additive Model
]
.pull-right[
```{r stats-gam-exec, echo = FALSE, eval = TRUE, ref.label = "stats-gam", cache = TRUE}
```
]
---
# Coordinate Systems (Layer 6)
## Use logarithmic axes, label price in dollars
.pull-left[
```{r stats-coord, echo = TRUE, eval = FALSE}
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(size = 0.5, alpha = 0.5) +
geom_smooth(method = "gam") +
scale_x_log10() + #<<
scale_y_log10(labels = scales::dollar) + #<<
labs(title = "Diamonds: Price by Carat (weight)",
subtitle = "Axes on Base 10 Log Scale\nSmoothing Method: gam",
x = "Carat", y = "Price",
caption = "Diamonds dataset, ggplot2")
```
* More options for transformations:
*scale_x_continuous(trans = ...)*
* Create custom transformations via *scales::trans_new()*
]
.pull-right[
```{r stats-coord-exec, echo = FALSE, eval = TRUE, ref.label = "stats-coord", cache = TRUE}
```
]
---
# Themes (Layer 7)
## Theme Classic
```{r ggplot-p, echo = FALSE, eval = TRUE}
p <- ggplot(diamonds, aes(x = carat, y = price)) +
geom_point(size = 0.5, alpha = 0.5) +
geom_smooth(method = "gam") +
scale_x_log10() + #<<
scale_y_log10(labels = scales::dollar) + #<<
labs(title = "Diamonds: Price by Carat (weight)",
subtitle = "Axes on Base 10 Log Scale\nSmoothing Method: gam",
x = "Carat", y = "Price",
caption = "Diamonds dataset, ggplot2")
# Increasing font sizes
```
.pull-left[
```{r theme-classic, echo = TRUE, eval = FALSE, cache = TRUE}
p + theme_classic()
```
* We define the plot using `p <- ggplot(...)`.
* Then we can simply define a theme using `p + theme_xxx()`.
]
.pull-right[
```{r theme-classic-exec, echo = FALSE, eval = TRUE, cache = TRUE}
p + theme_classic(base_size = 18)
```
]
---
# Themes (Layer 7)
## Theme Dark
```{r, echo = FALSE}
theme_set(theme_dark(base_size = 18))
```
.pull-left[
```{r theme-dark, echo = TRUE, eval = FALSE, cache = TRUE}
p + theme_dark()
```
See more themes: Type *?theme_* in RStudio
]
.pull-right[
```{r theme-dark-exec, echo = FALSE, eval = TRUE, cache = TRUE}
p + theme_dark(base_size = 18)
```
]
---
# Themes (Layer 7)
## More pre-defined themes: ggthemes
.pull-left[
```{r theme-wsj, echo = TRUE, eval = FALSE, cache = TRUE}
library(ggthemes)
p + theme_wsj()
```
* theme_wsj(): Based on plots in
*The Wall Street Journal*
* More themes: See packages
**hrbrthemes** and **ggtech**
]
.pull-right[
```{r theme-wsj-exec, echo = FALSE, eval = TRUE, cache = TRUE}
p + theme_wsj()
```
]
---
# Themes (Layer 7)
## A Simple Theme Customization
```{r, echo = FALSE}
theme_set(theme_gray(base_size = 18))
```
.pull-left[
```{r theme-custom, echo = TRUE, eval = FALSE}
p + theme(
panel.background = element_blank(),
panel.grid = element_blank(),
axis.text = element_text(colour = "darkblue"))
```
* Starting point: Default theme theme_gray()
* Remove gray background
* Remove grid lines
* Change font color for axes labels to dark blue
]
.pull-right[
```{r theme-custom-exec, echo = FALSE, eval = TRUE, ref.label = "theme-custom", cache = TRUE}
```
]
---
class: center, middle
# Thanks!
### Youtube: StatistikinDD
### Twitter: @StatistikinDD
### github: fjodor
Slides created via the R package [**xaringan**](https://github.com/yihui/xaringan).
The chakra comes from [remark.js](https://remarkjs.com), [**knitr**](https://yihui.org/knitr), and [R Markdown](https://rmarkdown.rstudio.com).
Thanks to **Yihui Xie** for *{knitr}* and *{xaringan}* and **Garrick Aden-Buie** for *{xaringanthemer}*.