forked from libjohn/rfun_flipped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercises_02_viz_answers.Rmd
119 lines (84 loc) · 2.01 KB
/
exercises_02_viz_answers.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
---
title: "Visualize Data in R with ggplot2"
subtitle: "Exercises"
abstract: "These exercises are adapated in whole or in part based on the <i>Master the Tidyverse</i> work by Garrett Grolemund at RStudio. \nCC BY Garrett Grolemund, RStudio ; BY-NC John Little"
---
<center>[CC BY-NC 4.0](https://creativecommons.org/licenses/by-nc/4.0/)</center>
```{r setup}
library(tidyverse)
```
```{r}
mpg
```
## Your Turn 1
Run the code on the slide to make a graph. Pay strict attention to spelling, capitalization, and parentheses!
```{r}
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
```
## Your Turn 2
Add `color`, `size`, `alpha`, and `shape` aesthetics to your graph. Experiment.
```{r fig.height=7}
mpg %>%
ggplot() +
geom_point(mapping = aes(x = displ, y = hwy,
color = class,
size = cyl,
shape = drv,
alpha = hwy))
```
## Your Turn 3
Replace this scatterplot with one that draws boxplots. Use the cheatsheet. Try your best guess.
```{r}
mpg %>%
ggplot() +
geom_point(aes(class, hwy))
mpg %>%
ggplot() +
geom_boxplot(aes(class, hwy))
```
## Your Turn 4
Make a histogram of the `hwy` variable from `mpg`.
```{r}
mpg %>%
ggplot() +
geom_histogram(aes(hwy))
```
```{r}
mpg %>%
ggplot() +
geom_histogram(aes(hwy), binwidth = 2)
```
## Your Turn 5
Make a density plot of `hwy` colored by `class`.
```{r}
mpg %>%
ggplot() +
geom_density(mapping = aes(x = hwy, color = class))
```
## Your Turn 6
Make a bar chart `hwy` colored by `class`.
```{r}
mpg %>%
ggplot() +
geom_bar(mapping = aes(x = class, color = class))
```
```{r}
mpg %>%
ggplot() +
geom_bar(mapping = aes(x = class, fill = class))
```
## Your Turn 7
Predict what this code will do. Then run it.
```{r}
mpg %>%
ggplot() +
geom_point(aes(displ, hwy)) +
geom_smooth(aes(displ, hwy))
```
## Your Turn 8
Save the last plot.
```r
ggsave("mylastplot.png")
# or right-click the image
```