-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lesson2.Rmd
404 lines (323 loc) · 9.74 KB
/
Lesson2.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
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
---
params:
lesson: "Lesson 2"
title: "Reading data and plotting facets and curves"
bookchapter_name: "Section 3.5.1"
bookchapter_section: "https://r4ds.had.co.nz/data-visualisation.html#facets"
functions: "`facet_wrap`, `facet_grid`, `geom_smooth`, `filter`"
packages: "`ggplot2`,`dplyr`"
# end inputs ---------------------------------------------------------------
header-includes: \usepackage{float}
always_allow_html: yes
output:
html_document:
code_folding: hide
---
```{r, setup, echo = FALSE, cache = FALSE, include = FALSE}
options(width=100)
knitr::opts_chunk$set(
eval = FALSE, # run all code
echo = TRUE, # show code chunks in output
tidy = TRUE, # make output as tidy
message = FALSE, # mask all messages
warning = FALSE, # mask all warnings
comment = "",
tidy.opts=list(width.cutoff=100), # set width of code chunks in output
size="small" # set code chunk size
)
```
\
<!-- install packages -->
```{r, load packages, eval=T, include=T, cache=F, message=F, warning=F, results='hide',echo=F}
packages <- c("ggplot2","ggthemes","dplyr","tidyverse","zoo","RColorBrewer","viridis","plyr")
if (require(packages)) {
install.packages(packages,dependencies = T)
require(packages)
# load tvthemes
devtools::install_github("Ryo-N7/tvthemes")
}
lapply(packages,library,character.only=T)
```
<!-- ____________________________________________________________________________ -->
<!-- ____________________________________________________________________________ -->
<!-- ____________________________________________________________________________ -->
<!-- start body -->
# `r paste0(params$lesson,": ",params$title)`
\
Functions for `r params$lesson`
`r params$functions`
\
Packages for `r params$lesson`
`r params$packages`
\
# Agenda
[Data visualisation in `R` for Data Science, `r params$bookchapter_name`](`r params$bookchapter_section`).
<!-- end yaml template------------------------------------------------------- -->
* Do first problem set
* Read in data file
* Plotting facets
* Plotting curves
* Combining plot types
\
# Do First problem set
Before each new session, we'll do a quick recap, called a Do First. These will only use functions we've previously covered, so if you're unsure or can't remember, just check the code from the previous session.
\
Recreate the below plot using the smaller NYC Airbnb dataset (`nyc` from Lesson 1). There are four aesthetics to change and the plot uses `theme_solarized`.
_Hint_: Use the help `?` function if something isn't clear.
```{r, echo=T}
# You didn't think we'd make it this easy, did you?
```
```{r, echo=F, eval=T, out.width="100%"}
require(ggplot2)
require(dplyr)
require(ggthemes)
# smaller csv file (16 cols)
url <- "http://data.insideairbnb.com/united-states/ny/new-york-city/2021-04-07/data/listings.csv.gz"
nyc <- read_csv(url)
nyc <- nyc[nyc$id < 20000,] # get smaller subet of data
ggplot(data=nyc) +
geom_point(mapping = aes(x=minimum_nights, y=number_of_reviews),
colour = "orange",
shape = 15,
size = 2,
alpha = 0.4) +
theme_solarized()
```
\
# Some useful shortkeys for making `R` life easier
`TAB` = autofill rest of function/global variable
`CTRL + ENTER` = run code
`ALT + minus sign` = insert assign operator `<-`
`CTRL + SHIFT + M` = insert pipe `%>%`
**Run `ALT + SHIFT + K` for [all available shortkeys](https://support.rstudio.com/hc/en-us/articles/200711853-Keyboard-Shortcuts)** \
# Read in data
<!-- NOT RUN -->
<!-- create emailable airbnb dataset -->
```{r,echo=F,eval=F}
nyc_compact <- nyc[1:1000,]
write_csv(nyc_compact,paste0(getwd(),"/nyc_compact.csv"))
```
\
```{r}
my_file <- "your_csv_file.csv"
my_data <- read_csv(my_file) # read in the csv data file
glimpse(my_data)
```
\
# Grouping data
One way to group your data is by colour
```{r, eval = T}
my_data <- mpg
my_theme <- theme_classic()
ggplot(data = my_data) +
geom_point(mapping = aes(x = displ, y = hwy, colour = class)) +
my_theme
```
\
# Plotting facets
`facet_wrap` and `facet_grid`
Facets add a third variable to a plot
The facet function takes a formula as an argument, which is just a data structure, denoted by a tilde **~**
`facet_wrap`
When you have one variable to plot as a facet
```{r,eval=T}
ggplot(data = my_data) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_wrap(~ class, nrow = 2) +
my_theme
```
`facet_grid`
When you know the two variables you want to plot
**The formula structure for `facet_grid` is Y variable `~` X variable, e.g. `drv ~ cyl`**
```{r, eval=T}
ggplot(data = my_data) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ cyl) +
my_theme
```
You can also replace the X or Y argument in `facet_grid` with a period (".") to plot only one variable.
```{r, eval=T}
# Y var
ggplot(data = my_data) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(. ~ cyl) +
my_theme
# X var
ggplot(data = my_data) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(drv ~ .) +
my_theme
```
# Exercise 3.5.1
[Try the exercises from 3.5.1](https://r4ds.had.co.nz/data-visualisation.html#exercises-2).
```{r, echo=F}
#.1
my_data <- mpg
my_theme <- theme_classic()
my_data %>% names
ggplot(data = my_data) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(cty ~ .) +
my_theme
#2.
ggplot(data = mpg) +
geom_point(mapping = aes(x = drv, y = cyl))
ggplot(data = mpg) +
geom_point(mapping = aes(x = drv, y = cyl)) +
facet_grid(drv ~ cyl)
#3.
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)
#4.
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class)) +
facet_wrap(~ class, nrow = 2)
#6.
# when unique variable is in col
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(. ~ class)
# when unique variable is in row it flip the axis
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy)) +
facet_grid(class ~ .)
```
------
# `geom_smooth`
Plotting points (`geom_point`) or lines (`geom_smooth`)
```{r,eval=T}
# left
ggplot(data = my_data) +
geom_point(mapping = aes(x = displ, y = hwy)) +
my_theme
# right
ggplot(data = my_data) +
geom_smooth(mapping = aes(x = displ, y = hwy)) +
my_theme
```
# Linetype
Grouping by linetype
```{r, eval=T}
ggplot(data = my_data) +
geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv)) +
my_theme
```
# Group vs. colour
Using `group` separates the data into objects ...
```{r,eval=T}
ggplot(data = my_data) +
geom_smooth(mapping = aes(x = displ, y = hwy)) +
my_theme
ggplot(data = my_data) +
geom_smooth(mapping = aes(x = displ, y = hwy, group = drv)) +
my_theme
```
\
... but `colour` will distinguish the differences among these objects
```{r, eval=T}
ggplot(data = my_data) +
geom_smooth(
mapping = aes(x = displ, y = hwy, colour = drv),
show.legend = FALSE
) +
my_theme
```
# Geometric objects
# Adding `geoms`
Possibly the most useful part of plotting data is layering plot types
```{r,eval=T}
ggplot(data = my_data) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth(mapping = aes(x = displ, y = hwy)) +
my_theme
# condensing code
ggplot(data = my_data, mapping = aes(x = displ, y = hwy)) +
geom_point() +
geom_smooth() +
my_theme
# adding aes
ggplot(data = my_data, mapping = aes(x = displ, y = hwy)) +
geom_point(colour = "steel blue") +
geom_smooth(colour = "#C6BDEA", fill="#C6BDEA") +
my_theme
```
\
But why does this throw an error?
```{r, eval=T, error=T}
# adding aes
ggplot(data = my_data) +
geom_point(mapping = aes(x = displ, y = hwy)) +
geom_smooth() +
my_theme
```
Specifying layers
```{r, eval=T}
ggplot(data = my_data, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) +
geom_smooth() +
my_theme
```
\
Applying different datasets to one plot (overriding data)
```{r, eval=T}
require(dplyr)
names(my_data)
# subset data with filter
my_data_subcompact <- filter(filter(my_data, class == "subcompact"))
ggplot(data = my_data, mapping = aes(x = displ, y = hwy)) +
geom_point(mapping = aes(color = class)) + # original data
geom_smooth(data = my_data_subcompact, se = FALSE) + # filtered data
my_theme
```
\
# Exercise 3.6.1
[Try the exercises from 3.6.1](https://r4ds.had.co.nz/data-visualisation.html#exercises-3).
```{r}
#1
ggplot(my_data) # ...
#2
```
```{r, echo=F}
#1
ggplot(my_data, aes(displ, hwy)) +
geom_point() +
geom_smooth(se=F) +
my_theme
#2
ggplot(my_data, aes(displ, hwy)) +
geom_point() +
geom_smooth(aes(group = drv), se=F) +
my_theme
#3
ggplot(my_data, aes(displ, hwy, color = drv)) +
geom_point() +
geom_smooth(se=F) +
my_theme
#4
ggplot(my_data, aes(displ, hwy)) +
geom_point(aes(colour=drv)) +
geom_smooth(se=F) +
my_theme
#5
ggplot(my_data) +
geom_point(aes(displ, hwy, color = drv)) +
geom_smooth(aes(displ, hwy, linetype=drv), se=F) +
my_theme
#6
# notice stroke argument
ggplot(my_data) +
geom_point(aes(displ, hwy), colour = "white",size=3) +
geom_point(aes(displ, hwy, colour = drv))
```
\
------
# Applying the Airbnb data
Use the new examples on the Airbnb dataset.
<!-- end body -->
<!-- ____________________________________________________________________________ -->
<!-- ____________________________________________________________________________ -->
<!-- ____________________________________________________________________________ -->