-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhw1.Rmd
319 lines (192 loc) · 6.27 KB
/
hw1.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
---
title: "BIOS 611 HW1 Data visualisation (Chapter 3)"
author: "Matthew J Rich"
date: "`r format(Sys.time(), '%m/%d/%Y')`"
output: html_document
---
*This homework is due September 5th 6pm.*
(This set of exercise is mostly taken from R for Data Science by Garrett Grolemund and Hadley Wickham.)
# Exercise 1
1. What's gone wrong with this code? Why are the points not blue?
```{r}
library(ggplot2)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))
```
Answer: plot is not blue because manual asctetics must be defined outside AES()
```{r}
library(ggplot2)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))
```
2. Which variables in `mpg` are categorical? Which variables are continuous?
(Hint: type `?mpg` to read the documentation for the dataset). How
can you see this information when you run `mpg`?
Answer: only one column contains real numbers so that is the only continuous data out of the 11 variables present
```{r}
library(ggplot2)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = "blue"))
```
3. Map a continuous variable to `color`, `size`, and `shape`. How do
these aesthetics behave differently for categorical vs. continuous
variables?
Answer: Only color and size can be mapped to a continuous variable
```{r}
```
4. What happens if you map the same variable to multiple aesthetics?
Answer: you will generate two legends one for each mapping
```{r}
library(ggplot2)
ggplot(data=mpg) +
lugeom_point(mapping = aes(x = hwy, y = cty, color = displ, size = displ))
```
5. What does the `stroke` aesthetic do? What shapes does it work with?
(Hint: use `?geom_point`)
Answer:
```{r}
```
6. What happens if you map an aesthetic to something other than a variable
name, like `aes(colour = displ < 5)`?
Answer:
```{r}
```
# Exercise 2
1. What do the empty cells in plot with `facet_grid(drv ~ cyl)` mean?
How do they relate to this plot?
```{r, eval = FALSE}
ggplot(data = mpg) +
geom_point(mapping = aes(x = drv, y = cyl))
```
Answer:
```{r}
```
2. What plots does the following code make? What does `.` do?
```{r eval = FALSE}
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ .)
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(. ~ cyl)
```
Answer:
```{r}
```
3. Take the first faceted plot in this section:
```{r, eval = FALSE}
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2)
```
What are the advantages to using faceting instead of the colour aesthetic?
What are the disadvantages? How might the balance change if you had a
larger dataset?
Answer:
```{r}
```
# Exercise 3
1. What geom would you use to draw a line chart? A boxplot?
A histogram? An area chart?
Answer:
```{r}
```
2. Run this code in your head and predict what the output will look like.
Then, run the code in R and check your predictions.
```{r, eval = FALSE}
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) +
geom_point() +
geom_smooth(se = FALSE)
```
Answer:
```{r}
```
3. What does `show.legend = FALSE` do? What happens if you remove it?
Why do you think I used it earlier in the chapter?
Answer:
```{r}
```
4. What does the `se` argument to `geom_smooth()` do?
Answer:
```{r}
```
5. Will these two graphs look different? Why/why not?
```{r, eval = FALSE}
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth()
ggplot() +
geom_point(data = mpg, mapping = aes(x = displ, y = hwy)) +
geom_smooth(data = mpg, mapping = aes(x = displ, y = hwy))
```
Answer:
```{r}
```
6. Recreate the R code necessary to generate the following graphs.
![Smooth lines for each `drv`](`r "https://github.com/datasci611/datasci_src/blob/master/HW1P0306A.png?raw=true"`)
Answer:
```{r, echo=TRUE, eval=TRUE}
```
![A single smooth line, transparency by `year`](`r "https://github.com/datasci611/datasci_src/blob/master/HW1P0306B.png?raw=true"`)
Answer:
```{r, echo=TRUE, eval=TRUE}
```
![Layered dots and an additional text information](`r "https://github.com/datasci611/datasci_src/blob/master/HW1P0306C.png?raw=true"`)
Adding texts was not covered in class, but give it a try!
Answer:
```{r, echo=TRUE, eval=TRUE}
```
# Exercise 4
1. 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?
Answer:
```{r}
```
2. What does `geom_col()` do? How is it different to `geom_bar()`?
Answer:
```{r}
```
3. What variables does `stat_smooth()` compute? What parameters control
its behaviour?
Answer:
```{r}
```
# Exercise 5
1. What is the problem with this plot? How could you improve it?
```{r}
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
geom_point()
```
Answer:
```{r}
```
2. What parameters to `geom_jitter()` control the amount of jittering?
Answer:
```{r}
```
3. Compare and contrast `geom_jitter()` with `geom_count()`.
Answer:
```{r}
```
# Exercise 6
1. Turn a stacked bar chart into a pie chart using `coord_polar()`.
Answer:
```{r}
```
2. What does `labs()` do? Read the documentation.
Answer:
```{r}
```
3. What does the plot below tell you about the relationship between city
and highway mpg? Why is `coord_fixed()` important? What does
`geom_abline()` do?
```{r, fig.asp = 1, out.width = "50%"}
ggplot(data = mpg, mapping = aes(x = cty, y = hwy)) +
geom_point() +
geom_abline() +
coord_fixed()
```
Answer:
```{r}
```