-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy path11_partIII_visualization.R
More file actions
388 lines (302 loc) · 11 KB
/
11_partIII_visualization.R
File metadata and controls
388 lines (302 loc) · 11 KB
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
# load package
library(ggplot2) # for plotting
library(pryr) # for profiling
library(bench) # for profiling
library(fs) # for profiling
# random numbers generation
x <- rnorm(10^6, mean=5)
y <- 1 + 1.4*x + rnorm(10^6)
plotdata <- data.frame(x=x, y=y)
object_size(plotdata)
# generate scatter plot
splot <-
ggplot(plotdata, aes(x=x, y=y))+
geom_point()
object_size(splot)
mem_used()
system.time(print(splot))
mem_used()
ggsave("splot.pdf", device="pdf", width = 5, height = 5)
file_size("splot.pdf")
# generate scatter plot
splot2 <-
ggplot(plotdata, aes(x=x, y=y))+
geom_point(pch=".")
mem_used()
system.time(print(splot2))
mem_used()
# install.packages("scattermore")
library(scattermore)
# generate scatter plot
splot3 <-
ggplot()+
geom_scattermore(aes(x=x, y=y), data=plotdata)
# show plot in interactive session
system.time(print(splot3))
# plot to file
ggsave("splot3.pdf", device="pdf", width = 5, height = 5)
file_size("splot3.pdf")
# generate scatter plot
splot4 <-
ggplot(plotdata, aes(x=x, y=y))+
geom_hex()
mem_used()
system.time(print(splot4))
mem_used()
# SET UP----
# see 05_aggregtion_visualization.Rmd for details
# load packages
library(data.table)
library(ggplot2)
# import data into RAM (needs around 200MB)
taxi <- fread("data/tlc_trips.csv",
nrows = 1000000)
# first, we remove the empty vars V8 and V9
taxi$V8 <- NULL
taxi$V9 <- NULL
# clean the factor levels
taxi$Payment_Type <- tolower(taxi$Payment_Type)
taxi$Payment_Type <- factor(taxi$Payment_Type, levels = unique(taxi$Payment_Type))
# load packages
library(ggplot2)
# set up the canvas
taxiplot <- ggplot(taxi, aes(y=Tip_Amt, x= Fare_Amt))
taxiplot
# simple x/y plot
taxiplot + geom_scattermore(pointsize = 3)
# simple x/y plot
taxiplot + geom_scattermore(pointsize = 3, alpha=0.2)
# two-dimensional bins
taxiplot + geom_bin2d()
# two-dimensional bins
taxiplot +
stat_bin_2d(geom="point",
mapping= aes(size = log(after_stat(count)))) +
guides(fill = "none")
# compute frequency of per tip amount and payment method
taxi[, n_same_tip:= .N, by= c("Tip_Amt", "Payment_Type")]
frequencies <- unique(taxi[Payment_Type %in% c("credit", "cash"),
c("n_same_tip",
"Tip_Amt",
"Payment_Type")][order(n_same_tip,
decreasing = TRUE)])
# plot top 20 frequent tip amounts
fare <- ggplot(data = frequencies[1:20], aes(x = factor(Tip_Amt),
y = n_same_tip))
fare + geom_bar(stat = "identity")
fare + geom_bar(stat = "identity") +
facet_wrap("Payment_Type")
# indicate natural numbers
taxi[, dollar_paid := ifelse(Tip_Amt == round(Tip_Amt,0), "Full", "Fraction"),]
# extended x/y plot
taxiplot +
geom_scattermore(pointsize = 3, alpha=0.2, aes(color=Payment_Type)) +
facet_wrap("dollar_paid") +
theme(legend.position="bottom")
taxi[, rounded_up := ifelse(Fare_Amt + Tip_Amt == round(Fare_Amt + Tip_Amt, 0),
"Rounded up",
"Not rounded")]
# extended x/y plot
taxiplot +
geom_scattermore(data= taxi[Payment_Type == "credit"],
pointsize = 3, alpha=0.2, aes(color=rounded_up)) +
facet_wrap("dollar_paid") +
theme(legend.position="bottom")
modelplot <- ggplot(data= taxi[Payment_Type == "credit" &
dollar_paid == "Fraction" &
0 < Tip_Amt],
aes(x = Fare_Amt, y = Tip_Amt))
modelplot +
geom_scattermore(pointsize = 3, alpha=0.2, color="darkgreen") +
geom_smooth(method = "lm", colour = "black") +
theme(legend.position="bottom")
modelplot <- ggplot(data= taxi[Payment_Type == "credit"
& dollar_paid == "Fraction"
& 0 < Tip_Amt],
aes(x = Fare_Amt, y = Tip_Amt))
modelplot +
geom_scattermore(pointsize = 3, alpha=0.2, color="darkgreen") +
geom_smooth(method = "lm", colour = "black") +
ylab("Amount of tip paid (in USD)") +
xlab("Amount of fare paid (in USD)") +
theme_bw(base_size = 18, base_family = "serif")
modelplot <- ggplot(data= taxi[Payment_Type == "credit"
& dollar_paid == "Fraction"
& 0 < Tip_Amt],
aes(x = Fare_Amt, y = Tip_Amt))
modelplot +
geom_scattermore(pointsize = 3, alpha=0.2, color="darkgreen") +
geom_smooth(method = "lm", colour = "black") +
ylab("Amount of tip paid (in USD)") +
xlab("Amount of fare paid (in USD)") +
theme_bw(base_size = 18, base_family = "serif") +
theme(axis.title = element_text(face="bold"))
# 'define' a new theme
theme_my_serif <-
theme_bw(base_size = 18, base_family = "serif") +
theme(axis.title = element_text(face="bold"))
# apply it
modelplot +
geom_scattermore(pointsize = 3, alpha=0.2, color="darkgreen") +
geom_smooth(method = "lm", colour = "black") +
ylab("Amount of tip paid (in USD)") +
xlab("Amount of fare paid (in USD)") +
theme_my_serif
# 'define' a new theme
my_serif_theme <-
theme_bw(base_size = 18, base_family = "serif") +
theme(axis.title = element_text(face="bold"), complete = TRUE)
# apply it
modelplot +
geom_scattermore(pointsize = 3, alpha=0.2, color="darkgreen") +
geom_smooth(method = "lm", colour = "black") +
ylab("Amount of tip paid (in USD)") +
xlab("Amount of fare paid (in USD)") +
theme_my_serif
# define own theme
theme_my_serif <-
function(base_size = 15,
base_family = "",
base_line_size = base_size/170,
base_rect_size = base_size/170){
# use theme_bw() as a basis but replace some design elements
theme_bw(base_size = base_size,
base_family = base_family,
base_line_size = base_size/170,
base_rect_size = base_size/170) %+replace%
theme(
axis.title = element_text(face="bold")
)
}
# apply the theme
modelplot +
geom_scattermore(pointsize = 3, alpha=0.2, color="darkgreen") +
geom_smooth(method = "lm", colour = "black") +
ylab("Amount of tip paid (in USD)") +
xlab("Amount of fare paid (in USD)") +
theme_my_serif(base_size = 18, base_family="serif")
# load GIS packages
library(rgdal)
library(rgeos)
# download the zipped shapefile to a temporary file; unzip
BASE_URL <-
"https://www1.nyc.gov/assets/planning/download/zip/data-maps/open-data/"
FILE <- "nycd_19a.zip"
URL <- paste0(BASE_URL, FILE)
tmp_file <- tempfile()
download.file(URL, tmp_file)
file_path <- unzip(tmp_file, exdir= "data")
# delete the temporary file
unlink(tmp_file)
# read GIS data
nyc_map <- readOGR(file_path[1], verbose = FALSE)
# have a look at the GIS data
summary(nyc_map)
# transform the projection
p <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
nyc_map <-
spTransform(nyc_map, p)
# check result
summary(nyc_map)
nyc_map <- fortify(nyc_map)
# taxi trips plot data
taxi_trips <- taxi[Start_Lon <= max(nyc_map$long) &
Start_Lon >= min(nyc_map$long) &
End_Lon <= max(nyc_map$long) &
End_Lon >= min(nyc_map$long) &
Start_Lat <= max(nyc_map$lat) &
Start_Lat >= min(nyc_map$lat) &
End_Lat <= max(nyc_map$lat) &
End_Lat >= min(nyc_map$lat)
]
taxi_trips <- taxi_trips[base::sample(1:nrow(taxi_trips), 50000)]
taxi_trips$start_time <- lubridate::hour(taxi_trips$Trip_Pickup_DateTime)
# define new variable for facets
taxi_trips$time_of_day <- "Morning"
taxi_trips[start_time > 12 & start_time < 17]$time_of_day <- "Afternoon"
taxi_trips[start_time %in% c(17:24, 0:5)]$time_of_day <- "Evening/Night"
taxi_trips$time_of_day <-
factor(taxi_trips$time_of_day,
levels = c("Morning", "Afternoon", "Evening/Night"))
# set up the canvas
locations <- ggplot(taxi_trips, aes(x=long, y=lat))
# add the map geometry
locations <- locations + geom_map(data = nyc_map,
map = nyc_map,
aes(map_id = id))
locations
# add pick-up locations to plot
locations +
geom_scattermore(aes(x=Start_Lon, y=Start_Lat),
color="orange",
pointsize = 1,
alpha = 0.2)
# add drop-off locations to plot
locations +
geom_scattermore(aes(x=End_Lon, y=End_Lat),
color="steelblue",
pointsize = 1,
alpha = 0.2) +
geom_scattermore(aes(x=Start_Lon, y=Start_Lat),
color="orange",
pointsize = 1,
alpha = 0.2)
# pick-up locations
locations +
geom_scattermore(aes(x=Start_Lon, y=Start_Lat),
color="orange",
pointsize =1,
alpha = 0.2) +
facet_wrap(vars(time_of_day))
# drop-off locations
locations +
geom_scattermore(aes(x=End_Lon, y=End_Lat),
color="steelblue",
pointsize = 1,
alpha = 0.2) +
facet_wrap(vars(time_of_day))
# drop-off locations
locations +
geom_scattermore(aes(x=End_Lon, y=End_Lat, color = start_time),
pointsize = 1,
alpha = 0.2) +
scale_colour_gradient2( low = "red", mid = "yellow", high = "red",
midpoint = 12)
# drop-off locations
locations +
geom_scattermore(aes(x=End_Lon, y=End_Lat, color = start_time ),
pointsize = 1,
alpha = 0.2)
# indicate natural numbers
taxi[, dollar_paid := ifelse(Tip_Amt == round(Tip_Amt,0),
"Full",
"Fraction"),]
# extended x/y plot
taxiplot +
geom_scattermore(alpha=0.2,
pointsize=3,
aes(color=Payment_Type)) +
facet_wrap("dollar_paid") +
theme(legend.position="bottom")
# indicate natural numbers
taxi[, dollar_paid := ifelse(Tip_Amt == round(Tip_Amt,0),
"Full",
"Fraction"),]
# extended x/y plot
taxiplot +
geom_scattermore(alpha=0.2, pointsize = 3,
aes(color=Payment_Type)) +
facet_wrap("dollar_paid") +
scale_color_discrete(type = c("red",
"steelblue",
"orange",
"purple")) +
theme(legend.position="bottom")
try(detach("package:ggplot2", unload=TRUE, force = TRUE))
try(detach("package:data.table", unload=TRUE, force = TRUE))
try(detach("package:scattermore", unload=TRUE, force = TRUE))
try(detach("package:pryr", unload=TRUE, force = TRUE))
try(detach("package:bench", unload = TRUE, force = TRUE))
try(detach("package:fs", unload= TRUE, force = TRUE))
try(detach("package:rgdal", unload= TRUE, force = TRUE))
try(detach("package:rgeos", unload= TRUE, force = TRUE))