-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCH09.qmd
More file actions
577 lines (458 loc) · 15.2 KB
/
CH09.qmd
File metadata and controls
577 lines (458 loc) · 15.2 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
```{r}
#| include: false
library(tidyverse)
library(nycflights13)
```
### Exercises (Chapter 9)
1. Create a scatterplot of `hwy` vs. `displ` where the points are pink filled in triangles.
::: {.callout-note icon="false" title="Answer"}
```{r}
# Your R code here
```
:::
2. Why did the following code not result in a plot with blue points?
```{r}
#| fig-show: hide
#| fig-alt: |
#| Scatterplot of highway fuel efficiency versus engine size of cars
#| that shows a negative association. All points are red and
#| the legend shows a red point that is mapped to the word blue.
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, color = "blue"))
```
::: {.callout-note icon="false" title="Answer"}
```{r}
# Proper R code here
```
*Your text answer here.*
:::
3. What does the `stroke` aesthetic do? What shapes does it work with? (Hint: use `?geom_point`)
::: {.callout-note icon="false" title="Answer"}
```{r}
mpg |>
ggplot(aes(x = displ, y = hwy)) +
geom_point(shape = 21, stroke = 0.5) -> p1
mpg |>
ggplot(aes(x = displ, y = hwy)) +
geom_point(shape = 21, stroke = 1) -> p2
mpg |>
ggplot(aes(x = displ, y = hwy)) +
geom_point(shape = 21, stroke = 2) -> p3
library(patchwork)
p1 / p2 / p3
```
*Your text answer here.*
:::
4. What happens if you map an aesthetic to something other than a variable name, like `aes(color = displ < 5)`? Note, you'll also need to specify x and y.
::: {.callout-note icon="false" title="Answer"}
```{r}
mpg |>
ggplot(aes(x = displ, y = hwy, color = displ < 5)) +
geom_point()
```
*Your text answer here.*
:::
5. What geom would you use to draw a line chart? A boxplot? A histogram? An area chart?
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
```{r}
# R Code here
```
:::
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
```{r}
# R code here
```
:::
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
```{r}
# R code here
```
:::
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
```{r}
# Youe R code here
```
:::
6. Earlier in this chapter we used `show.legend` without explaining it:
```{r}
#| fig-show: hide
#| message: false
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_smooth(aes(color = drv), show.legend = FALSE)
```
What does `show.legend = FALSE` do here? What happens if you remove it? Why do you think we used it earlier?
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
```{r}
#| warning: false
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_smooth(aes(color = drv), show.legend = FALSE) -> p1
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_smooth(aes(color = drv), show.legend = TRUE) -> p2
p1 / p2
```
:::
7. What does the `se` argument to `geom_smooth()` do?
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
```{r}
#| warning: false
ggplot(mpg, aes(x = displ, y = hwy, color = drv)) +
geom_smooth(se = FALSE)
```
:::
8. Recreate the R code necessary to generate the following graphs. Note that wherever a categorical variable is used in the plot, it's `drv`.
```{r}
#| echo: false
#| message: false
#| layout-ncol: 2
#| fig-width: 2.5
#| fig-height: 2.5
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth(se = FALSE)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_smooth(aes(group = drv), se = FALSE) +
geom_point()
ggplot(mpg, aes(x = displ, y = hwy, color = drv)) +
geom_point() +
geom_smooth(se = FALSE)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(aes(color = drv)) +
geom_smooth(se = FALSE)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(aes(color = drv)) +
geom_smooth(aes(linetype = drv), se = FALSE)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(size = 4, color = "white") +
geom_point(aes(color = drv))
```
::: {.callout-note icon="false" title="Answer"}
The code for each of the plots is given below.
```{r}
#| echo: true
#| eval: false
# Your R code here
```
:::
9. What happens if you facet on a continuous variable?
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
```{r}
mpg |>
ggplot(aes(x = drv, y = cyl)) +
geom_point() +
facet_wrap(~hwy)
```
:::
10. What do the empty cells in the plot above with `facet_grid(drv ~ cyl)` mean? Run the following code. How do they relate to the resulting plot?
```{r}
#| fig-show: hide
ggplot(mpg) +
geom_point(aes(x = drv, y = cyl))
```
::: {.callout-note icon="false" title="Answer"}
```{r}
ggplot(mpg) +
geom_point(aes(x = drv, y = cyl)) +
facet_grid(drv ~ cyl)
```
*Your text answer here.*
:::
11. What plots does the following code make? What does `.` do?
```{r}
#| fig-show: hide
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_grid(drv ~ .)
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_grid(. ~ cyl)
```
::: {.callout-note icon="false" title="Answer"}
```{r}
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_grid(drv ~ .)
```
*Your text answer here.*
```{r}
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_grid(. ~ cyl)
```
*Your text answer here.*
:::
12. Take the first faceted plot in this section:
```{r}
#| fig-show: hide
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_wrap(~ cyl, nrow = 2)
```
What are the advantages to using faceting instead of the color aesthetic? What are the disadvantages? How might the balance change if you had a larger dataset?
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
```{r}
# facet
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)
# color
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy, color = class))
# both
ggplot(mpg) +
geom_point(
aes(x = displ, y = hwy, color = class),
show.legend = FALSE) +
facet_wrap(~ class, nrow = 2)
# highlighting
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(color = "gray") +
geom_point(
data = mpg |> filter(class == "compact"),
color = "pink"
)
```
:::
13. Read `?facet_wrap`. What does `nrow` do? What does `ncol` do? What other options control the layout of the individual panels? Why doesn't `facet_grid()` have `nrow` and `ncol` arguments?
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
:::
14. Which of the following plots makes it easier to compare engine size (`displ`) across cars with different drive trains? What does this say about when to place a faceting variable across rows or columns?
```{r}
#| fig-show: hide
#| message: false
ggplot(mpg, aes(x = displ)) +
geom_histogram() +
facet_grid(drv ~ .)
ggplot(mpg, aes(x = displ)) +
geom_histogram() +
facet_grid(. ~ drv)
```
::: {.callout-note icon="false" title="Answer"}
```{r}
ggplot(mpg, aes(x = displ)) +
geom_histogram() +
facet_grid(drv ~ .)
ggplot(mpg, aes(x = displ)) +
geom_histogram() +
facet_grid(. ~ drv)
```
*Your text answer here.*
:::
15. Recreate the following plot using `facet_wrap()` instead of `facet_grid()`. How do the positions of the facet labels change?
```{r}
#| fig-show: hide
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_grid(drv ~ .)
```
::: {.callout-note icon="false" title="Answer"}
```{r}
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_grid(drv ~ .) -> p1
ggplot(mpg) +
geom_point(aes(x = displ, y = hwy)) +
facet_wrap(~drv, nrow = 3) -> p2
p1 + p2
```
*Your text answer here.*
:::
16. What is the default geom associated with `stat_summary()`? How could you rewrite the previous plot to use that geom function instead of the stat function?
```{r}
ggplot(diamonds) +
stat_summary(
aes(x = cut, y = depth),
fun.min = min,
fun.max = max,
fun = median
)
```
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
```{r}
diamonds |>
group_by(cut) |>
summarize(
lower = min(depth),
upper = max(depth),
midpoint = median(depth)
) |>
ggplot(aes(x = cut, y = midpoint)) +
geom_pointrange(aes(ymin = lower, ymax = upper))
```
:::
17. What does `geom_col()` do? How is it different from `geom_bar()`?
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
:::
18. Most geoms and stats come in pairs that are almost always used in concert. Make a list of all the pairs. What do they have in common? (Hint: Read through the documentation.)
::: {.callout-note icon="false" title="Answer"}
Geoms and stats that are almost always used in concert are listed below:
:::
| **geom** | **stat** |
|-------------------------|-------------------------|
| `geom_bar()` | `stat_count()` |
| `geom_bin2d()` | `stat_bin_2d()` |
| `geom_boxplot()` | `stat_boxplot()` |
| `geom_contour_filled()` | `stat_contour_filled()` |
| `geom_contour()` | `stat_contour()` |
| `geom_count()` | `stat_sum()` |
| `geom_density_2d()` | `stat_density_2d()` |
| `geom_density()` | `stat_density()` |
| `geom_dotplot()` | `stat_bindot()` |
| `geom_function()` | `stat_function()` |
| `geom_sf()` | `stat_sf()` |
| `geom_sf()` | `stat_sf()` |
| `geom_smooth()` | `stat_smooth()` |
| `geom_violin()` | `stat_ydensity()` |
| `geom_hex()` | `stat_bin_hex()` |
| `geom_qq_line()` | `stat_qq_line()` |
| `geom_qq()` | `stat_qq()` |
| `geom_quantile()` | `stat_quantile()` |
19. What variables does `stat_smooth()` compute? What arguments control its behavior?
::: {.callout-note icon="false" title="Answer"}
`stat_smooth()` computes the following variables:
- `y` or `x`: Predicted value
- `ymin` or `xmin`: Lower pointwise confidence interval around the mean
- `ymax` or `xmax`: Upper pointwise confidence interval around the mean
- `se`: Standard error
:::
20. In our proportion bar chart, we needed to set `group = 1`. Why? In other words, what is the problem with these two graphs?
```{r}
#| fig-show: hide
ggplot(diamonds, aes(x = cut, y = after_stat(prop))) +
geom_bar()
ggplot(diamonds, aes(x = cut, fill = color, y = after_stat(prop))) +
geom_bar()
```
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
:::
```{r}
#| layout-ncol: 2
# one variable
ggplot(diamonds, aes(x = cut,
y = after_stat(prop))) +
geom_bar()
ggplot(diamonds, aes(x = cut,
y = after_stat(prop),
group = 1)) +
geom_bar()
# two variables
ggplot(diamonds, aes(x = cut,
fill = color,
y = after_stat(prop))) +
geom_bar()
ggplot(diamonds, aes(x = cut,
fill = color,
y = after_stat(prop),
group = color)) +
geom_bar()
```
21. What is the problem with the following plot? How could you improve it?
```{r}
#| fig-show: hide
ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point()
```
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
:::
```{r}
#| layout-ncol: 2
ggplot(mpg, aes(x = cty, y = hwy)) +
geom_point()
ggplot(mpg, aes(x = cty, y = hwy)) +
geom_jitter()
```
22. What, if anything, is the difference between the two plots? Why?
```{r}
#| fig-show: hide
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point()
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(position = "identity")
```
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
:::
```{r}
#| layout-ncol: 2
# Your R code here
```
23. What parameters to `geom_jitter()` control the amount of jittering?
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
:::
```{r}
#| layout-ncol: 3
set.seed(321)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(color = "gray") +
geom_jitter(height = 1, width = 1)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(color = "gray") +
geom_jitter(height = 1, width = 5)
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_point(color = "gray") +
geom_jitter(height = 5, width = 1)
```
24. Compare and contrast `geom_jitter()` with `geom_count()`.
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
:::
```{r}
#| layout-ncol: 2
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_jitter()
ggplot(mpg, aes(x = displ, y = hwy)) +
geom_count()
```
25. What's the default position adjustment for `geom_boxplot()`? Create a visualization of the `mpg` dataset that demonstrates it.
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
:::
```{r}
#| layout-ncol: 2
ggplot(mpg, aes(x = drv, y = displ)) +
geom_boxplot()
ggplot(mpg, aes(x = drv, y = displ)) +
geom_boxplot(position = "dodge2")
```
26. Turn a stacked bar chart into a pie chart using `coord_polar()`.
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
:::
```{r}
#| layout-ncol: 2
# Your R code here
```
27. What's the difference between `coord_quickmap()` and `coord_map()`?
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
:::
28. What does the following plot tell you about the relationship between city and highway mpg? Why is `coord_fixed()` important? What does `geom_abline()` do?
```{r}
#| fig-show: hide
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
geom_point() +
geom_abline() +
coord_fixed()
```
::: {.callout-note icon="false" title="Answer"}
*Your text answer here.*
```{r}
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
geom_point() +
geom_abline() +
coord_fixed()
```
:::