-
Notifications
You must be signed in to change notification settings - Fork 2
/
solutions.Rmd
223 lines (155 loc) · 5.54 KB
/
solutions.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
---
title: "R Charting: Solutions"
author: ""
date: ""
output:
slidy_presentation:
highlight: tango
ioslides_presentation:
highlight: zenburn
keep_md: no
smaller: yes
widescreen: yes
---
```{r, include=FALSE}
knitr::opts_chunk$set(message=FALSE, warning=FALSE, cache=FALSE)
import::from("ggplot2", "aes", "mpg")
```
## Solutions: Section 1
### 1.1
```{r}
ggplot2::ggplot(mpg, aes(x = displ, y = cty))
```
### 1.2
```{r, echo=TRUE}
?mpg
```
### 1.3
Make a scatter plot of `hwy` vs. `cty`
```{r, fig.height=5}
ggplot2::ggplot(data = mpg, aes(x = hwy, y = cty)) +
ggplot2::geom_point()
```
### 1.4
Make a scatter plot of urban fuel consumption vs engine size (displacement).
```{r, fig.height=4}
ggplot2::ggplot(data = mpg, aes(x = displ, y = cty)) +
ggplot2::geom_point()
```
## Solutions: Section 2
### 2.1
What's wrong with this code? Why aren't the points blue?
<div class="columns-2">
```{r, fig.height=3, fig.width=5}
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = "blue")) +
ggplot2::geom_point()
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy)) +
ggplot2::geom_point(colour = "blue")
```
</div>
### 2.2
Map a continuous variable to `colour`, `size` and `shape`. How do these aesthetics behave differently for categorical vs. continuous variables? Hint: use `dplyr::glimpse(mpg)` to identify variable types.
<div class="columns-2">
```{r, fig.height=3, fig.width=4}
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = cty)) +
ggplot2::geom_point()
```
```{r, fig.height=3, fig.width=4}
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = drv)) +
ggplot2::geom_point()
```
</div>
### 2.3
What happens if you map the same variable to multiple aesthetics?
```{r,fig.height=5}
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = drv, shape = drv)) +
ggplot2::geom_point()
```
### 2.4
What happens if you map an aesthetic to something other than a variable name, like a Boolean statement `aes(colour = displ < 5)`
```{r, fig.height=3}
ggplot2::ggplot(data = mpg, aes(x = displ, y = hwy, colour = displ < 5)) +
ggplot2::geom_point()
```
## Solutions: Section 3
### 3.1
Using the `mpg` dataset create a histogram of `cty`. What impact do different values for the `bins` argument have?
```{r, eval=TRUE}
ggplot2::ggplot(data = mpg, aes(x = cty)) +
ggplot2::geom_histogram(bins = 100)
```
```{r, eval=TRUE}
ggplot2::ggplot(data = mpg, aes(x = cty)) +
ggplot2::geom_histogram(bins = 5)
```
### 3.2
Make a density plot of `cty` by `drv`. Use the `alpha` argument to make the overlapping regions visible.
```{r, fig.height = 2.7}
ggplot2::ggplot(data = mpg, aes(x = cty, fill = drv)) +
ggplot2::geom_density(alpha = 0.7)
```
```{r, fig.height = 2.7}
ggplot2::ggplot(data = mpg, aes(x = cty, fill = drv)) +
ggplot2::geom_density(alpha = 0.3)
```
### 3.3
Recreate the US unemployed population line graph. Add another line to show the total population.
```{r, fig.height = 3}
ggplot2::economics |>
ggplot2::ggplot(aes(x = date, y = unemploy)) +
ggplot2::geom_line() +
ggplot2::geom_line(aes(x = date, y = pop))
```
## Solutions: Section 4
### 4.1
Create a basic scatter plot showing highway fuel efficiency vs city fuel efficiency. Add a
theme_minimal; and colour the points based on the number of cylinders the car has.
```{r, fig.height = 3}
ggplot2::ggplot(data = mpg) +
ggplot2::geom_point(mapping = aes(x = hwy, y = cty, colour = as.factor(cyl))) +
ggplot2::theme_minimal()
```
### 4.2
Change the title and axis labels to something sensible based on what the chart is showing.
```{r, fig.height = 3}
ggplot2::ggplot(data = mpg) +
ggplot2::geom_point(mapping = aes(x = hwy, y = cty, colour = as.factor(cyl))) +
ggplot2::theme_minimal() +
ggplot2::labs(title = "Fuel efficiency comparison between city and highway",
x = "Highway efficiency",
y = "City efficiency")
```
### 4.3
Create a custom theme changing: major gridlines to grey, dashed; removing minor gridlines; x and y
axis text to black, pt 10, italic; move the legend position to the bottom of the plot.
```{r, fig.height = 3}
ggplot2::ggplot(data = mpg) +
ggplot2::geom_point(mapping = aes(x = hwy, y = cty, colour = as.factor(cyl))) +
ggplot2::labs(title = "Fuel efficiency comparison between city and highway",
x = "Highway efficiency",
y = "City efficiency") +
ggplot2::theme(panel.grid.major = ggplot2::element_line(colour = "grey", linetype = "dashed"),
panel.grid.minor = ggplot2::element_blank(),
axis.text = ggplot2::element_text(colour = "black", size = 10, face = "italic"),
legend.position = "bottom")
```
### 4.4
Instead of the custom theme, use the gss theme from `mojchart` and one of the built-in colour schemes.
```{r, fig.height = 3}
ggplot2::ggplot(data = mpg) +
ggplot2::geom_point(mapping = aes(x = hwy, y = cty, colour = as.factor(cyl))) +
ggplot2::labs(title = "Fuel efficiency comparison between city and highway",
x = "Highway efficiency",
y = "City efficiency") +
mojchart::scale_colour_moj(n = 4, scheme = "govanal_bars") +
mojchart::theme_gss(xlabel = TRUE)
```
```{r, fig.height = 3}
ggplot2::ggplot(data = mpg) +
ggplot2::geom_point(mapping = aes(x = hwy, y = cty, colour = as.factor(cyl))) +
ggplot2::labs(title = "Fuel efficiency comparison between city and highway",
x = "Highway efficiency",
y = "City efficiency") +
mojchart::scale_colour_moj(n = 4) +
mojchart::theme_gss(xlabel = TRUE)
```