-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path02_spatial-data.R
More file actions
678 lines (554 loc) · 18.7 KB
/
02_spatial-data.R
File metadata and controls
678 lines (554 loc) · 18.7 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
## ----message = FALSE, warning = FALSE, results = 'hide'--------------------------------------------------------
pkgs <- c("sf", "gstat", "mapview", "nngeo", "rnaturalearth", "dplyr", "areal",
"nomisr", "osmdata", "OpenStreetMap", "tidyr", "texreg", "downlit", "xml2")
lapply(pkgs, require, character.only = TRUE)
## ----message = FALSE, warning = FALSE, results = 'hide'--------------------------------------------------------
pkgs <- c("tmap", "tmaptools", "viridisLite",
"ggplot2", "ggthemes", "rmapshaper", "cowplot")
lapply(pkgs, require, character.only = TRUE)
## --------------------------------------------------------------------------------------------------------------
sessionInfo()
## --------------------------------------------------------------------------------------------------------------
load("_data/msoa_spatial.RData")
load("_data/ulez_spatial.RData")
load("_data/pollution_spatial.RData")
load("_data/pubs_spatial.RData")
## --------------------------------------------------------------------------------------------------------------
st_crs(msoa.spdf) == st_crs(pol.spdf)
st_crs(msoa.spdf) == st_crs(pubs.spdf)
st_crs(msoa.spdf) == st_crs(ulez.spdf)
## --------------------------------------------------------------------------------------------------------------
# MSOA in different crs --> transform
pol.spdf <- st_transform(pol.spdf, crs = st_crs(msoa.spdf))
pubs.spdf <- st_transform(pubs.spdf, crs = st_crs(msoa.spdf))
ulez.spdf <- st_transform(ulez.spdf, crs = st_crs(msoa.spdf))
# Check if all geometries are valid, and make valid if needed
msoa.spdf <- st_make_valid(msoa.spdf)
## --------------------------------------------------------------------------------------------------------------
# Subset to pollution estimates in London
pol_sub.spdf <- pol.spdf[msoa.spdf, ] # or:
pol_sub.spdf <- st_filter(pol.spdf, msoa.spdf)
mapview(pol_sub.spdf)
## --------------------------------------------------------------------------------------------------------------
# Subset pubs to pubs not in the ulez area
sub2.spdf <- pubs.spdf[ulez.spdf, , op = st_disjoint] # or:
sub2.spdf <- st_filter(pubs.spdf, ulez.spdf, .predicate = st_disjoint)
mapview(sub2.spdf)
## --------------------------------------------------------------------------------------------------------------
msoa.spdf$ulez <- 0
# intersecting lsoas
within <- msoa.spdf[ulez.spdf,]
# use their ids to create binary indicator
msoa.spdf$ulez[which(msoa.spdf$MSOA11CD %in% within$MSOA11CD)] <- 1
table(msoa.spdf$ulez)
## --------------------------------------------------------------------------------------------------------------
# Assign MSOA to each point
pubs_msoa.join <- st_join(pubs.spdf, msoa.spdf, join = st_within)
# Count N by MSOA code (drop geometry to speed up)
pubs_msoa.join <- dplyr::count(st_drop_geometry(pubs_msoa.join),
MSOA11CD = pubs_msoa.join$MSOA11CD,
name = "pubs_count")
sum(pubs_msoa.join$pubs_count)
# Merge and replace NAs with zero (no matches, no pubs)
msoa.spdf <- merge(msoa.spdf, pubs_msoa.join,
by = "MSOA11CD", all.x = TRUE)
msoa.spdf$pubs_count[is.na(msoa.spdf$pubs_count)] <- 0
## --------------------------------------------------------------------------------------------------------------
# Use geometric centroid of each MSOA
cent.sp <- st_centroid(msoa.spdf[, "MSOA11CD"])
# Get K nearest neighbour with distance
knb.dist <- st_nn(cent.sp,
pubs.spdf,
k = 1, # number of nearest neighbours
returnDist = TRUE, # we also want the distance
progress = FALSE)
msoa.spdf$dist_pubs <- unlist(knb.dist$dist)
summary(msoa.spdf$dist_pubs)
## --------------------------------------------------------------------------------------------------------------
# Create buffer (1km radius)
cent.buf <- st_buffer(cent.sp,
dist = 1000) # dist in meters
mapview(cent.buf)
### New version (using areal package)
# We use area-weighted interpolation from the areal package
int.spdf <- aw_interpolate(
cent.buf, # interpolate to
tid = "MSOA11CD", # id of target
source = pol.spdf, # the source to be interpolated from
sid = "ukgridcode", # source id
weight = "sum", # function for interpolation
output = "sf", # output object
intensive = "no22011" # variables to be interpolated
)
# # ### Old version ("by hand")
# # Add area of each buffer (in this constant)
# cent.buf$area <- as.numeric(st_area(cent.buf))
#
# # Calculate intersection of pollution grid and buffer
# int.df <- st_intersection(cent.buf, pol.spdf)
# int.df$int_area <- as.numeric(st_area(int.df)) # area of intersection
#
# # And we use the percent overlap areas as the weights to calculate a weighted mean.
#
# # Area of intersection as share of buffer
# int.df$area_per <- int.df$int_area / int.df$area
#
# # Aggregate as weighted mean
# int.df <- st_drop_geometry(int.df)
# int.df$no2_weighted <- int.df$no22011 * int.df$area_per
# int.df <- aggregate(list(no2 = int.df[, "no2_weighted"]),
# by = list(MSOA11CD = int.df$MSOA11CD),
# sum)
## --------------------------------------------------------------------------------------------------------------
# Back to non-spatial df
int.df <- st_drop_geometry(int.spdf)
names(int.df)[2] <- "no2"
# Merge back to spatial data.frame
msoa.spdf <- merge(msoa.spdf, int.df, by = "MSOA11CD", all.x = TRUE)
mapview(msoa.spdf, zcol = "no2")
## --------------------------------------------------------------------------------------------------------------
# Define ethnic group shares
msoa.spdf$per_mixed <- msoa.spdf$KS201EW_200 / msoa.spdf$KS201EW0001 * 100
msoa.spdf$per_asian <- msoa.spdf$KS201EW_300 / msoa.spdf$KS201EW0001 * 100
msoa.spdf$per_black <- msoa.spdf$KS201EW_400 / msoa.spdf$KS201EW0001 * 100
msoa.spdf$per_other <- msoa.spdf$KS201EW_500 / msoa.spdf$KS201EW0001 * 100
# Define tenure
msoa.spdf$per_owner <- msoa.spdf$KS402EW_100 / msoa.spdf$KS402EW0001 * 100
msoa.spdf$per_social <- msoa.spdf$KS402EW_200 / msoa.spdf$KS402EW0001 * 100
# Non British passport
msoa.spdf$per_nonUK <- (msoa.spdf$KS205EW0001 - msoa.spdf$KS205EW0003)/ msoa.spdf$KS205EW0001 * 100
msoa.spdf$per_nonEU <- (msoa.spdf$KS205EW0001 - msoa.spdf$KS205EW0003 -
msoa.spdf$KS205EW0004 - msoa.spdf$KS205EW0005 -
msoa.spdf$KS205EW0006)/ msoa.spdf$KS205EW0001 * 100
msoa.spdf$per_nonUK_EU <- (msoa.spdf$KS205EW0005 + msoa.spdf$KS205EW0006)/ msoa.spdf$KS205EW0001 * 100
# Run regression
mod1.lm <- lm(no2 ~ per_mixed + per_asian + per_black + per_other +
per_owner + per_social + pubs_count + POPDEN + ulez,
data = msoa.spdf)
# summary
screenreg(list(mod1.lm), digits = 3)
## ----house-prices, cache=TRUE----------------------------------------------------------------------------------
# Download
hp.link <- "https://data.london.gov.uk/download/average-house-prices/bdf8eee7-41e1-4d24-90ce-93fe5cf040ae/land-registry-house-prices-MSOA.csv"
hp.df <- read.csv(hp.link)
hp.df <- hp.df[which(hp.df$Measure == "Median" &
grepl("2011", hp.df$Year)), ]
table(hp.df$Year)
# Aggregate across 2011 values
hp.df$med_house_price <- as.numeric(hp.df$Value)
hp.df <- aggregate(hp.df[, "med_house_price", drop = FALSE],
by = list(MSOA11CD = hp.df$Code),
FUN = function(x) mean(x, na.rm = TRUE))
# Merge spdf and housing prices
msoa.spdf <- merge(msoa.spdf, hp.df,
by = "MSOA11CD",
all.x = TRUE, all.y = FALSE)
hist(log(msoa.spdf$med_house_price))
## --------------------------------------------------------------------------------------------------------------
# Save
save(msoa.spdf, file = "_data/msoa2_spatial.RData")
## --------------------------------------------------------------------------------------------------------------
# Define colours
cols <- viridis(n = 7, direction = 1, option = "C")
mp1 <- tm_shape(msoa.spdf) +
tm_polygons(
fill = "no2", # variable for fill colouring
fill_alpha = 1, # transparency
fill.scale = tm_scale_intervals(
style = "fisher", # algorithm to def cut points
n = 7, # Number of requested cut points
values = cols # colouring
),
fill.legend = tm_legend(
title = "NO2",
hist = FALSE
)
) +
tm_borders(
col = "white",
lwd = 0.5,
fill_alpha = 0.5
)
mp1
## --------------------------------------------------------------------------------------------------------------
# Define colours
cols <- viridis(n = 7, direction = 1, option = "C")
mp1 <- tm_shape(msoa.spdf) +
tm_polygons(
fill = "no2",
fill_alpha = 1,
fill.scale = tm_scale_intervals(
style = "fisher",
n = 7,
values = cols
),
fill.legend = tm_legend(
title = "NO2",
hist = FALSE
)
) +
tm_borders(
col = "white",
lwd = 0.5,
fill_alpha = 0.5
) +
tm_shape(ulez.spdf) +
tm_borders(
col = "red",
lwd = 1,
fill_alpha = 1
)
mp1
## --------------------------------------------------------------------------------------------------------------
# Define colours
cols <- viridis(n = 7, direction = 1, option = "C")
mp1 <- tm_shape(msoa.spdf) +
tm_polygons(
fill = "no2",
fill_alpha = 1,
fill.scale = tm_scale_intervals(
style = "fisher",
n = 7,
values = cols
),
fill.legend = tm_legend(
title = expression('in'~mu*'g'/m^{3}),
hist = FALSE
)
) +
tm_borders(
col = "white",
lwd = 0.5,
fill_alpha = 0.5
) +
tm_shape(ulez.spdf) +
tm_borders(
col = "red",
lwd = 1,
fill_alpha = 1
) +
tm_layout(
frame = FALSE,
legend.frame = TRUE,
legend.bg.color = "white",
legend.position = c("right", "bottom"),
legend.outside = FALSE,
legend.title.size = 0.8,
legend.text.size = 0.8
) +
tm_title_out(
text = "NO2",
position = tm_pos_out("center", "top", # top cell
pos.h = "center"), # position in cell
size = 1.6
)
mp1
## ----warning=FALSE---------------------------------------------------------------------------------------------
# Save old projection
crs_orig <- st_crs(msoa.spdf)
# Change projection
ulez.spdf <- st_transform(ulez.spdf, 4326)
msoa.spdf <- st_transform(msoa.spdf, 4326)
# Get OSM data for background
osm_tmp <- read_osm(st_bbox(msoa.spdf), ext = 1.1, type = "osm-german")
# Define colours
cols <- viridis(n = 7, direction = 1, option = "C")
mp1 <- tm_shape(osm_tmp) +
tm_rgb() +
tm_shape(msoa.spdf) +
tm_polygons(
fill = "no2",
fill_alpha = 0.8,
fill.scale = tm_scale_intervals(
style = "fisher",
n = 7,
values = cols
),
fill.legend = tm_legend(
title = expression('in'~mu*'g'/m^{3}),
hist = FALSE
)
) +
tm_shape(ulez.spdf) +
tm_borders(
col = "red",
lwd = 1,
fill_alpha = 1
) +
tm_layout(
frame = FALSE,
legend.frame = TRUE,
legend.bg.color = "white",
legend.position = c("right", "bottom"),
legend.outside = FALSE,
legend.title.size = 0.8,
legend.text.size = 0.8
) +
tm_title_out(
text = "NO2",
position = tm_pos_out("center", "top", pos.h = "center"),
size = 1.6
)
mp1
## --------------------------------------------------------------------------------------------------------------
# Define colours
cols1 <- viridis(n = 7, direction = 1, option = "C")
# Define colours
cols2 <- viridis(n = 7, direction = 1, option = "D")
# First map: NO2
mp1 <- tm_shape(osm_tmp) +
tm_rgb() +
tm_shape(msoa.spdf) +
tm_polygons(
fill = "no2",
fill_alpha = 0.8,
fill.scale = tm_scale_intervals(
style = "fisher",
n = 7,
values = cols1
),
fill.legend = tm_legend(
title = expression('in'~mu*'g'/m^{3}),
hist = FALSE
)
) +
tm_shape(ulez.spdf) +
tm_borders(
col = "red",
lwd = 1,
fill_alpha = 1
) +
tm_layout(
frame = FALSE,
legend.frame = TRUE,
legend.bg.color = "white",
legend.position = c("right", "bottom"),
legend.outside = FALSE,
legend.title.size = 0.8,
legend.text.size = 0.8
) +
tm_title_out(
text = "NO2",
position = tm_pos_out("center", "top", pos.h = "center"),
size = 1.4
)
# Second map: % Black
mp2 <- tm_shape(osm_tmp) +
tm_rgb() +
tm_shape(msoa.spdf) +
tm_polygons(
fill = "per_black",
fill_alpha = 0.8,
fill.scale = tm_scale_intervals(
style = "fisher",
n = 7,
values = cols2
),
fill.legend = tm_legend(
title = "% black",
hist = FALSE
)
) +
tm_shape(ulez.spdf) +
tm_borders(
col = "red",
lwd = 1,
fill_alpha = 1
) +
tm_layout(
frame = FALSE,
legend.frame = TRUE,
legend.bg.color = "white",
legend.position = c("right", "bottom"),
legend.outside = FALSE,
legend.title.size = 0.8,
legend.text.size = 0.8
) +
tm_title_out(
text = "Ethnic Black inhabitants",
position = tm_pos_out("center", "top", pos.h = "center"),
size = 1.4
)
# Arrange side by side
tmap_arrange(mp1, mp2, ncol = 2, nrow = 1)
## --------------------------------------------------------------------------------------------------------------
png(file = paste("London.png", sep = ""), width = 14, height = 7, units = "in",
res = 100, bg = "white")
par(mar=c(0,0,3,0))
par(mfrow=c(1,1),oma=c(0,0,0,0))
tmap_arrange(mp1, mp2, ncol = 2, nrow = 1)
dev.off()
## --------------------------------------------------------------------------------------------------------------
gp <- ggplot(msoa.spdf)+
geom_sf(aes(fill = no2))+
scale_fill_viridis_c(option = "B")+
coord_sf(datum = NA)+
theme_map()+
theme(legend.position = c(.9, .6))
gp
## --------------------------------------------------------------------------------------------------------------
# Get some larger scale boundaries
borough.spdf <- st_read(dsn = paste0("_data", "/statistical-gis-boundaries-london/ESRI"),
layer = "London_Borough_Excluding_MHW" # Note: no file ending
)
# transform to only inner lines
borough_inner <- ms_innerlines(borough.spdf)
# Plot with inner lines
gp <- ggplot(msoa.spdf)+
geom_sf(aes(fill = no2), color = NA)+
scale_fill_viridis_c(option = "A")+
geom_sf(data = borough_inner, color = "gray92")+
geom_sf(data = ulez.spdf, color = "red", fill = NA)+
coord_sf(datum = NA)+
theme_map()+
labs(fill = "NO2")+
theme(legend.position = c(.9, .6))
gp
## --------------------------------------------------------------------------------------------------------------
sub4.spdf <- msoa.spdf[ulez.spdf, ]
## --------------------------------------------------------------------------------------------------------------
gp <- ggplot(msoa.spdf)+
geom_sf(aes(fill = per_asian))+
scale_fill_viridis_c(option = "E")+
coord_sf(datum = NA)+
theme_map()+
theme(legend.position = c(.9, .6))
gp
## --------------------------------------------------------------------------------------------------------------
### Distance to city center
# Define centre
centre <- st_as_sf(data.frame(lon = -0.128120855701165,
lat = 51.50725909644806),
coords = c("lon", "lat"),
crs = 4326)
# Reproject
centre <- st_transform(centre, crs = st_crs(msoa.spdf))
# Calculate distance
msoa.spdf$dist_centre <- as.numeric(st_distance(msoa.spdf, centre)) / 1000
# hist(msoa.spdf$dist_centre)
## --------------------------------------------------------------------------------------------------------------
# Define colours
cols <- viridis(n = 10, direction = 1, option = "B")
cols2 <- viridis(n = 10, direction = 1, option = "E")
library(tmap)
# Map 1: Distance - Fisher
mp1 <- tm_shape(msoa.spdf) +
tm_polygons(
fill = "dist_centre",
fill_alpha = 1,
fill.scale = tm_scale_intervals(
style = "fisher",
n = 10,
values = cols
),
fill.legend = tm_legend(
title = "Distance",
hist = FALSE
)
) +
tm_borders(col = "white", lwd = 0.5, fill_alpha = 0.5) +
tm_layout(
frame = FALSE,
legend.frame = TRUE,
legend.bg.color = "white",
legend.position = c("right", "bottom"),
legend.outside = FALSE,
legend.title.size = 0.8,
legend.text.size = 0.8
) +
tm_title_out(
text = "Dist centre",
position = tm_pos_out("center", "top", pos.h = "center"),
size = 1.6
)
# Map 2: Distance - Quantile
mp2 <- tm_shape(msoa.spdf) +
tm_polygons(
fill = "dist_centre",
fill_alpha = 1,
fill.scale = tm_scale_intervals(
style = "quantile",
n = 10,
values = cols
),
fill.legend = tm_legend(
title = "Distance",
hist = FALSE
)
) +
tm_borders(col = "white", lwd = 0.5, fill_alpha = 0.5) +
tm_layout(
frame = FALSE,
legend.frame = TRUE,
legend.bg.color = "white",
legend.position = c("right", "bottom"),
legend.outside = FALSE,
legend.title.size = 0.8,
legend.text.size = 0.8
) +
tm_title_out(
text = "Dist centre",
position = tm_pos_out("center", "top", pos.h = "center"),
size = 1.6
)
# Map 3: Pubs - Fisher
mp3 <- tm_shape(msoa.spdf) +
tm_polygons(
fill = "pubs_count",
fill_alpha = 1,
fill.scale = tm_scale_intervals(
style = "fisher",
n = 10,
values = cols
),
fill.legend = tm_legend(
title = "Count",
hist = FALSE
)
) +
tm_borders(col = "white", lwd = 0.5, fill_alpha = 0.5) +
tm_layout(
frame = FALSE,
legend.frame = TRUE,
legend.bg.color = "white",
legend.position = c("right", "bottom"),
legend.outside = FALSE,
legend.title.size = 0.8,
legend.text.size = 0.8
) +
tm_title_out(
text = "Pubs",
position = tm_pos_out("center", "top", pos.h = "center"),
size = 1.6
)
# Map 4: Pubs - Quantile
mp4 <- tm_shape(msoa.spdf) +
tm_polygons(
fill = "pubs_count",
fill_alpha = 1,
fill.scale = tm_scale_intervals(
style = "quantile",
n = 10,
values = cols
),
fill.legend = tm_legend(
title = "Count",
hist = FALSE
)
) +
tm_borders(col = "white", lwd = 0.5, fill_alpha = 0.5) +
tm_layout(
frame = FALSE,
legend.frame = TRUE,
legend.bg.color = "white",
legend.position = c("right", "bottom"),
legend.outside = FALSE,
legend.title.size = 0.8,
legend.text.size = 0.8
) +
tm_title_out(
text = "Pubs",
position = tm_pos_out("center", "top", pos.h = "center"),
size = 1.6
)
tmap_arrange(mp1, mp2, mp3, mp4, ncol = 2, nrow = 2)