-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlesson_plan.R
244 lines (186 loc) · 6.1 KB
/
lesson_plan.R
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
### Welcom to Shree and Dre's tidyverse overview
library(tidyverse)
library(nycflights13)
## cheat sheets: https://www.rstudio.com/resources/cheatsheets/
## Import data
# explain read.csv() vs read_csv() (use url -- put dataset on github and read it)
# mention write_csv()
## Clean dataset
# inspect data: head(), tail(), summary(), names(), unique(), briefly mention tibble vs df
# remove spaces
# change case
# rename columns
# reorder columns
## Wrangles
# filter() -select
# -mutate
# group_by()/summarise
# gather/spread
# join / bind_cols / bind_rows
## plots
## talk about grammar of graphics
## talk about aesthetics within each plot
## in prettify: talk about facet, smooth, reordering
# histogram: color vs fill, alpha,
# bar: factor level reorder
# geom_boxplot + geom_violin(): overlaying points with alpha
# scatter + geom_smooth(): point size, line type, size inside vs outside aes(), scale_x_log10()
############################################
## Import ##
############################################
# You can read in directly from files online
crime <- read_csv("https://vincentarelbundock.github.io/Rdatasets/csv/Ecdat/Crime.csv")
# Datasets need documentation too
browseURL("https://vincentarelbundock.github.io/Rdatasets/doc/Ecdat/Crime.html")
############################################
## Clean ##
############################################
# inspect data: head(), tail(), summary(), names(), unique(), briefly mention tibble vs df
# remove spaces
# change case
# rename columns
# reorder columns
# Take a look at subset of rows
head(crime)
tail(crime)
sample_frac(crime, 0.01)
# Get an overview of the data
names(crime)
summary(crime)
# Subset, reorder, clean data
select(crime) # Subset columns, can also be used to rename and reorder cols
filter(crime) # Subset rows
rename(crime) # Rename
drop_na(crime) # Remove all rows with any NA's
############################################
## Plot 1 ##
############################################
## What times are busiest?
## Wrangle data
wrangled_hr <- flights %>%
select(hour, minute)
wrangled_hr
## Plot
wrangled_hr %>%
ggplot() +
geom_histogram(aes(hour))
## Prettify plot
wrangled_hr %>%
ggplot() +
geom_histogram(aes(hour), binwidth = 1) +
theme_minimal()
############################################
## Plot 2 ##
############################################
## What times are busiest?
## Wrangle data
wrangled_min <- wrangled_hr %>%
mutate(
time_min = 60 * hour + minute,
time = time_min / 60
)
wrangled_min
## Plot
wrangled_min %>%
ggplot() +
geom_histogram(aes(time), binwidth = 1)
## Prettify plot
wrangled_min %>%
ggplot() +
geom_histogram(aes(time), binwidth = 1/4) +
labs(x = "Time (hour)", y = "Number of Flights", title = "Busiest departure times at New York airports") +
theme_minimal()
############################################
## Plot 3 ##
############################################
## Which airlines experience the most delays?
## Wrangle data
left_join(flights, airlines, by = "carrier") %>%
filter(is.na(flights$arr_delay) == FALSE) %>%
sample_frac(0.1) %>%
ggplot() +
geom_jitter(height = 0.3, size = 0.1, aes(arr_delay, name, alpha = 1/10, color = name)) +
theme_classic() +
theme(legend.position = "none")
left_join(flights, airlines, by = "carrier") %>%
filter(is.na(flights$arr_delay) == FALSE) %>%
sample_frac(0.1) %>%
ggplot(aes(name, arr_delay, color = name, fill = name)) +
geom_boxplot(aes(alpha = 1/5)) +
geom_jitter(position = position_jitter(width = 0, height = 0.1), alpha = 1/4) +
# geom_jitter(width = 0.1, aes(color = name)) +
theme(legend.position = "none") +
scale_y_log10() +
coord_flip()
############################################
## Plot 4 ##
############################################
## How does time of year affect flight delay?
season_df <- flights %>%
select(month, day, arr_delay, dep_delay, origin, dest, carrier) %>%
filter(arr_delay >= 0, dep_delay >= 0) %>%
group_by(origin, month, day) %>%
summarise(avg_delay = mean(arr_delay, na.rm = TRUE) + mean(dep_delay, na.rm = TRUE)) %>%
ungroup()
season_df %>%
group_by(origin) %>%
summarise(max(avg_delay), min(avg_delay))
season_df$date <- with(season_df, ISOdate(year = 2013, month, day))
season_df %>%
ggplot(aes(date, avg_delay)) +
geom_point(aes(color = origin)) +
scale_color_manual(values = c("springgreen", "purple", "dodgerblue")) +
geom_smooth(color = "magenta") +
ylab("average delay (min)") +
theme_classic()
season_df %>%
ggplot(aes(date, avg_delay)) +
geom_point(aes(color = origin, alpha = 1/8)) +
geom_smooth(aes(color = origin), se = FALSE) +
scale_color_manual(values = c("springgreen", "purple", "dodgerblue")) +
ylab("average delay (min)") +
theme_classic()
## let's facet
season_df %>%
ggplot(aes(date, avg_delay)) +
geom_point(aes(color = origin, alpha = 1/8)) +
geom_smooth(color = "blue", se = FALSE) +
scale_color_manual(values = c("springgreen", "purple", "dodgerblue")) +
ylab("average delay (min)") +
facet_grid(~origin)
season_df %>%
ggplot(aes(date, avg_delay)) +
geom_smooth(aes(color = origin), se = FALSE) +
scale_color_manual(values = c("springgreen", "purple", "dodgerblue")) +
ylab("average delay (min)") +
theme_classic()
############################################
## Plot 5 ##
############################################
## Which destinations are serviced by the top x airlines?
## Wrangle data
top_carriers <- flights %>%
count(carrier) %>%
arrange(-n) %>%
head(5) %>%
pull(carrier)
top_dest <- flights %>%
select(carrier, dest) %>%
filter(carrier %in% top_carriers) %>%
count(carrier, dest) %>%
spread(carrier, n) %>%
drop_na %>%
pull(dest)
wrangled_dest <- flights %>%
select(carrier, dest) %>%
filter(
carrier %in% top_carriers,
dest %in% top_dest
) %>%
inner_join(airlines)
wrangled_dest
## Plot
wrangled_dest %>%
ggplot() +
geom_bin2d(aes(dest, name))
## Prettify plot