forked from vtinney/EDF-GWU-Hyperlocal-HIA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBay_01.bc.estimates.global.R
710 lines (596 loc) · 30 KB
/
Bay_01.bc.estimates.global.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
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# Combined all run files
# ==========================================================================================================================================
# Created: 2019-11-14
#
# All read-in files are rasters with the following characteristics:
# 2367, 2909, 6885603 (nrow, ncol, ncell)
# -123.6325, -121.2083, 36.8925, 38.865 (xmin, xmax, ymin, ymax)
# 0.0008333333, 0.0008333333 (x, y)
# CRS: +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
#
#==================================================================
#Set working directory and load files
setwd('/GWSPH/home/vtinney/results3/bc/')
library(raster)
library(rgdal)
library(dplyr)
library(ggplot2)
library(ggspatial)
library(scales)
library(extrafont)
library(maptools)
loadfonts()
library(dplyr)
library(sf)
library(OpenStreetMap)
library(rJava)
library(gridExtra)
library(rgeos)
library(ggpubr)
library(spatialEco)
library(grid)
# ==================================================================
# Specify where the files are
pops <- '/GWSPH/home/vtinney/pop1/'
rates <- '/GWSPH/home/vtinney/rates1/'
concs <- '/GWSPH/home/vtinney/conc1/'
poptotal <- '/GWSPH/home/vtinney/pop1/'
shps <- '/GWSPH/home/vtinney/clip1/'
#bay.ext <- 'Extent: -121.208, -123.533, 36.893, 38.864'
#ala.ext <- 'Extent: -122.342, -121.469, 37.454, 37.906'
#oak.ext <- 'Extent: -122.328, -122.148, 37.716, 37.832'
#wo.ext <- 'Extent: -122.328, -122.253, 37.791, 37.832'
#ext <- c(bay.ext, ala.ext, oak.ext, wo.ext)
cbg.groups <- c('bay.cbg')
city.groups <- c('baycities')
co.groups <- c('bayco')
theme_map <- function(...) {
theme(
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
plot.title=element_text(hjust = 0, size=11,family="DejaVu Sans Light"),
plot.subtitle=element_text(hjust=0, size=9,family="DejaVu Sans Light"),
plot.caption = element_text(hjust=0, size=7,family="DejaVu Sans Light"),
legend.title=element_text(size=11, family="DejaVu Sans Light"),
legend.text=element_text(size=11, family="DejaVu Sans Light"),
axis.title=element_blank(),
legend.position = 'bottom',
legend.justification='center',
legend.spacing = unit(c(-.1,0.2,.2,0.2), "cm"),
panel.border = element_rect(colour = "black", fill=NA, size=0.5),
rect = element_blank())
}
#///////////////////////////////////////////////////////////////////////////////////////////////////
# Functions
myZonal <- function (x, z, stat, digits = 0, na.rm = TRUE,
...) {
library(data.table)
fun <- match.fun(stat)
vals <- getValues(x)
zones <- round(getValues(z), digits = digits)
rDT <- data.table(vals, z=zones)
setkey(rDT, z)
rDT[, lapply(.SD, fun, na.rm = TRUE), by=z]
}
ZonalPipe<- function (zone.in, raster.in, shp.out=NULL, stat){
require(raster)
require(rgdal)
require(plyr)
# Load raster
r <- raster.in
# Load zone shapefile
shp <- zone.in
# Project 'zone' shapefile into the same coordinate system than the input raster
shp <- spTransform(shp, crs(r))
# Add ID field to Shapefile
shp@data$ID<-c(1:length(shp@data[,1]))
# Crop raster to 'zone' shapefile extent
r <- crop(r, extent(shp))
# Rasterize shapefile
zone <- rasterize(shp, r, field="ID", dataType = "INT1U") # Change dataType if nrow(shp) > 255 to INT2U or INT4U
# Zonal stats
Zstat<-data.frame(myZonal(r, zone, stat))
colnames(Zstat)<-c("ID", paste0(names(r), "_", c(1:(length(Zstat)-1)), "_",stat))
# Merge data in the shapefile and write it
shp@data <- plyr::join(shp@data, Zstat, by="ID")
if (is.null(shp.out)){
return(shp)
}else{
writeOGR(shp, shp.out, layer= sub("^([^.]*).*", "\\1", basename(zone.in)), driver="ESRI Shapefile")
}
}
#///////////////////////////////////////////////////////////////////////////////////////////////////
# ==================================================================
# All health outcomes and BC
# ==================================================================
beta.groups <- c(0.006975614,0.003992021,0.008959741, #all cause mortality
0.006975614,0.003992021,0.008959741,
0.006975614,0.003992021,0.008959741, #cvd mortality
0.006975614,0.003992021,0.008959741,
0.019920424,0.007738012,0.031550078) # CVD hospitalizations
names(beta.groups) <- c('Janssen et al. 2011, point estimate', #1
'Janssen et al. 2011, lower CI',
'Janssen et al. 2011, upper CI',
'Janssen et al. 2011, point estimate', #4
'Janssen et al. 2011, lower CI',
'Janssen et al. 2011, upper CI',
'Janssen et al. 2011, point estimate', #7
'Janssen et al. 2011, lower CI',
'Janssen et al. 2011, upper CI',
'Janssen et al. 2011, point estimate', #10
'Janssen et al. 2011, lower CI',
'Janssen et al. 2011, upper CI',
'Peng et al. 2009, point estimate', #13
'Peng et al. 2009, lower CI',
'Peng et al. 2009, upper CI')
outcome.groups <- c('All-cause mortality',
'All-cause mortality',
'All-cause mortality',
'All-cause mortality',
'All-cause mortality',
'All-cause mortality',
'CVD mortality',
'CVD mortality',
'CVD mortality',
'CVD mortality',
'CVD mortality',
'CVD mortality',
'CVD hospitalizations',
'CVD hospitalizations',
'CVD hospitalizations')
rate.groups <- c('co.25',
'cbg.25',
'cvd.co.25',
'cvd.cbg.25',
'cvd.ha.65')
names(rate.groups) <- c('ages 25-99 years, County baseline disease rates',
'ages 25-99 years, CBG baseline disease rates',
'ages 25-99 years, County baseline disease rates',
'ages 25-99 years, CBG baseline disease rates',
'ages 65-99 years, County baseline disease rates')
conc.groups <- c('bc','bc.med','bc.min')
names(conc.groups) <- c('van Donkelaar et al. 2016','van Donkelaar et al. 2016 median concentrations','van Donkelaar et al. 2016 minimum concentrations')
pop.groups <- c('pop.ls.night.25','pop.ls.night.65')
names(pop.groups) <- c('LandScan USA, GPWv4 age fractions',
'LandScan USA, GPWv4 age fractions')
#clip.groups <- c('Export_Output','oak')
#names(clip.groups) <- c('West and Downtown Oakland','Oakland')
pdf(NULL)
for (i in 1:length(beta.groups)){
print(beta.groups[i])
for (j in 1:length(conc.groups)){
print(conc.groups[j])
if(i == 1 | i == 2 | i == 3 | i == 7 | i == 8 | i == 9 | i == 13 | i == 14 | i == 15){
clip.groups <- c('wo_outline','oak', 'alaco2', 'bay')
names(clip.groups) <- c('West and Downtown Oakland','Oakland','Alameda County','Bay area')
}
if(i == 4 | i == 5 | i == 6 | i == 10 | i == 11 | i == 12){
clip.groups <- c('wo_outline','oak','alaco2')
names(clip.groups) <- c('West and Downtown Oakland','Oakland','Alameda County')
}
for (m in 1:length(clip.groups)){
print(clip.groups[m])
if(i == 1 | i == 2 | i == 3){
b <- raster(paste(rates,rate.groups[1],'.tif',sep=''))
c = raster(paste(pops,pop.groups[1],'.tif',sep=''))
rate.names <- names(rate.groups[1])
pop.names <- names(pop.groups[1])
}
if(i == 4 | i == 5 | i == 6){
b <- raster(paste(rates,rate.groups[2],'.tif',sep=''))
c = raster(paste(pops,pop.groups[1],'.tif',sep=''))
rate.names <- names(rate.groups[2])
pop.names <- names(pop.groups[1])
}
if(i == 7 | i == 8 | i == 9) {
b <- raster(paste(rates,rate.groups[3],'.tif',sep=''))
c = raster(paste(pops,pop.groups[1],'.tif',sep=''))
rate.names <- names(rate.groups[3])
pop.names <- names(pop.groups[1])
}
if(i == 10 | i == 11 | i == 12){
b <- raster(paste(rates,rate.groups[4],'.tif',sep=''))
c = raster(paste(pops,pop.groups[1],'.tif',sep=''))
rate.names <- names(rate.groups[4])
pop.names <- names(pop.groups[1])
}
if(i == 13 | i == 14 | i == 15){
b <- raster(paste(rates,rate.groups[5],'.tif',sep=''))
c = raster(paste(pops,pop.groups[2],'.tif',sep=''))
rate.names <- names(rate.groups[5])
pop.names <- names(pop.groups[2])
}
a = raster(paste(concs,'conc.',conc.groups[j],'.tif',sep=''))
a[a==0]<-NA
af <- 1-exp(-beta.groups[i]*a)
af2 <- af
af <- af*100
mr <- af2*b
mr[mr==0]<-NA
c[c==0]<- NA
hia = overlay(c, b, a, fun=function(r1, r2, r3){return(r1*r2*(10^-4)*(1-exp(-beta.groups[i]*r3)))})
hia[hia==0]<-NA
shp <- readOGR(dsn=shps, layer=paste(clip.groups[m]))
crs(shp) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
shp.f <- fortify(shp) %>%
mutate(id = as.numeric(id))
af <- crop(af, shp)
af <- mask(af, shp)
af.iqr <- quantile(af)
af.iqr <- as.matrix(af.iqr)
af.iqr <- t(af.iqr)
af.mean <- cellStats(af, 'mean')
af.df <- cbind(af.mean, af.iqr)
print(paste(names(clip.groups[m]),', attributable fraction, ',names(conc.groups[j]),', ',names(beta.groups[i]),sep=''))
print(af.df)
f1 = paste('/GWSPH/home/vtinney/results3/bc/af/',names(clip.groups[m]),', ',outcome.groups[i],', ',names(conc.groups[j]),', ',names(beta.groups[i]),'.tif',sep='')
writeRaster(af, filename=f1, format="GTiff", overwrite=TRUE)
min.af <- minValue(af)
max.af <- maxValue(af)
min.af.label <- round(minValue(af),2)
max.af.label <- round(maxValue(af),2)
mean.af <- (min.af+max.af)/2
mean.af.label <- round(mean.af,2)
af.log <- log(af)
z.af <- scale(af.log)
af.df <- rasterToPoints(af)
af.df <- data.frame(af.df)
colnames(af.df) <- c('lon','lat','val')
base <- openmap(c(ymin(shp),xmin(shp)),c(ymax(shp),xmax(shp)),
type = "esri-topo",
mergeTiles = TRUE)
base <- openproj(base, projection = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
if((i == 1 | i == 4 | i == 7 | i == 10 | i == 13) & (j==1)){
autoplot(base) +
geom_polygon(data = shp.f, aes(x = long, y = lat, group = group),
fill="grey50",alpha=0.5)+
geom_tile(data=af.df,aes(lon, lat, fill = val),alpha=0.8) +
scale_fill_gradient2("Attributable Fraction (%)",
low = "#3ec267",
mid = "#fff429", #ff7e29
high = "#fc0339", ##ff1f40
midpoint = mean.af,
breaks=c(min.af,mean.af,max.af),
labels=c(min.af.label,mean.af.label,max.af.label),
limits=c(min.af, max.af),
na.value = 'grey50',
guide = guide_colourbar(
direction = "horizontal",
label=TRUE,
keyheight = unit(2, units = "mm"),
title.position = 'top',
title.hjust = 0.5,
label.hjust = 0.5,
barwidth = 15,
nrow = 1,
byrow = T,
label.position = "bottom"))+
theme_map()+
geom_path(data = shp.f, aes(x = long, y = lat, group = group),
color = "grey60", size = 0.5)+
labs(title='Attributable fraction',
caption=paste0(names(beta.groups[i]),', ',names(conc.groups[j]),'.',sep=''),
subtitle=paste0('Range: ',min.af.label,' to ',max.af.label,'%.'),sep='')
ggsave(paste0(names(clip.groups[m]),' AF ',outcome.groups[i],' ',names(beta.groups[i]),' ',names(conc.groups[j]),'.af.png',sep=''),dpi=320)
print('af')
}
#==============================================================================================================================================================
mr <- crop(mr, shp)
mr <- mask(mr, shp)
f2 = paste('/GWSPH/home/vtinney/results3/bc/mr/',names(clip.groups[m]),', ',outcome.groups[i],', ',names(conc.groups[j]),', ',names(beta.groups[i]),', ',rate.names,'.tif',sep='')
writeRaster(mr, filename=f2, format="GTiff", overwrite=TRUE)
min.mr <- minValue(mr)
min.mr.label <- round(minValue(mr),2)
max.mr <- maxValue(mr)
max.mr.label <- round(maxValue(mr),2)
mean.mr <- (min.mr+max.mr)/2
mean.mr.label <- round(mean.mr,2)
mr.df <- rasterToPoints(mr)
mr.df <- data.frame(mr.df)
colnames(mr.df) <- c('lon','lat','val')
if((i == 1 | i == 4 | i == 7 | i == 10 | i == 13)&(j==1)){
autoplot(base) +
geom_polygon(data = shp.f, aes(x = long, y = lat, group = group),
fill="grey50",alpha=0.5)+
geom_tile(data=mr.df,aes(lon, lat, fill = val),alpha=0.8) +
scale_fill_gradient2("Risk per 10,000",
low = "#3ec267",
mid = "#fff429", #ff7e29
high = "#fc0339", ##ff1f40
midpoint = mean.mr,
breaks=c(min.mr,mean.mr,max.mr),
labels=c(min.mr.label,mean.mr.label,max.mr.label),
limits=c(min.mr, max.mr),
na.value = 'grey50',
guide = guide_colourbar(
direction = "horizontal",
label=TRUE,
keyheight = unit(2, units = "mm"),
title.position = 'top',
title.hjust = 0.5,
label.hjust = 0.5,
barwidth = 15,
nrow = 1,
byrow = T,
label.position = "bottom"))+
theme_map()+
geom_path(data = shp.f, aes(x = long, y = lat, group = group),
color = "grey60", size = 0.5)+
labs(title=paste0('Risk of ',outcome.groups[i],sep=''),
caption=paste0(names(beta.groups[i]),', ',names(conc.groups[j]),', ',rate.names,'.',sep=''),
subtitle=paste0('Range: ',min.mr.label,' to ',max.mr.label,' per 10,000. '),sep='')
ggsave(paste0(names(clip.groups[m]),' MR ',outcome.groups[i],' ',names(beta.groups[i]),' ',names(conc.groups[j]),' ',rate.names,'.af.png',sep=''),dpi=320)
print('mr')
}
#===========================================================================================================================
# Crop and map HIA files
hia <- crop(hia, shp)
hia <- mask(hia, shp)
f3 = paste('/GWSPH/home/vtinney/results3/bc/paf/',names(clip.groups[m]),', ',outcome.groups[i],', ',names(conc.groups[j]),', ',names(beta.groups[i]),', ',rate.names,', ',pop.names,'.tif',sep='')
writeRaster(hia, filename=f3, format="GTiff", overwrite=TRUE)
hia.df <- rasterToPoints(hia)
hia.df <- data.frame(hia.df)
colnames(hia.df) <- c('lon','lat','val')
min.hia <- minValue(hia)
max.hia <- maxValue(hia)
min.hia.label <- round(minValue(hia),2)
max.hia.label <- round(maxValue(hia),2)
mean.hia <- (min.hia+max.hia)/2
mean.hia.label <- round(mean.hia,2)
if((i == 1 | i == 4 | i == 7 | i == 10 | i == 13)&(j==1)){
autoplot(base) +
geom_polygon(data = shp.f, aes(x = long, y = lat, group = group),
fill="grey50",alpha=0.5)+
geom_tile(data=hia.df,aes(lon, lat, fill = val),alpha=0.8) +
scale_fill_gradient2("Excess cases (n)",
low = "#3ec267",
mid = "#fff429", #ff7e29
high = "#fc0339", ##ff1f40
midpoint = mean.hia,
breaks=c(min.hia,mean.hia,max.hia),
labels=c(min.hia.label,mean.hia.label,max.hia.label),
limits=c(min.hia, max.hia),
na.value = 'grey50',
guide = guide_colourbar(
direction = "horizontal",
label=TRUE,
keyheight = unit(2, units = "mm"),
title.position = 'top',
title.hjust = 0.5,
label.hjust = 0.5,
barwidth = 15,
nrow = 1,
byrow = T,
label.position = "bottom"))+
theme_map()+
geom_path(data = shp.f, aes(x = long, y = lat, group = group),
color = "grey60", size = 0.5)+
labs(title=paste0(outcome.groups[i],' cases attributable to black carbon.',sep=''),
caption=paste0(names(beta.groups[i]),', ',names(conc.groups[j]),', ',rate.names,',\n',pop.names,'.',sep=''),
subtitle=paste0('Range: ',min.hia.label,' to ',max.hia.label,' per grid cell. '),sep='')
ggsave(paste0(names(clip.groups[m]),' PAF ',outcome.groups[i],' ',names(beta.groups[i]),' ',names(conc.groups[j]),' ',pop.names,' ',rate.names,'.af.png',sep=''),dpi=300)
print('hia')
}
#=========================================================================================================================
cbg.shp <- readOGR(dsn=shps, layer=paste(cbg.groups))
crs(cbg.shp) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
cbg.shp <- crop(cbg.shp, shp)
cbg.shp.f <- fortify(cbg.shp) %>%
mutate(id = as.numeric(id))
zone.in <- cbg.shp
raster.in <- hia
shp2 <- ZonalPipe(zone.in, raster.in, stat="sum")
shp2@data <- shp2@data %>% mutate(id = row.names(.))
shp_df <- fortify(shp2, region = "id")
shp_df <- shp_df %>% left_join(shp2@data, by = c("id"="id"))
shp_df <- as.data.frame(shp_df)
shp_df[,ncol(shp_df)][shp_df[,ncol(shp_df)] == 0] <- NA
r.min <- min(shp_df[,ncol(shp_df)],na.rm=TRUE)
r.max <- max(shp_df[,ncol(shp_df)],na.rm=TRUE)
r.med <- median(shp_df[,ncol(shp_df)],na.rm=TRUE)
colnames(shp_df)[ncol(shp_df)] <- 'hia.val'
r.mean <- (r.min+r.max)/2
r.mean.label <- round(r.mean,2)
r.min.label <- round(r.min,2)
r.max.label <- round(r.max,2)
r.med.label <- round(r.med,2)
zone.in <- cbg.shp
raster.in <- c
shp3 <- ZonalPipe(zone.in, raster.in, stat="sum")
shp3@data <- shp3@data %>% mutate(id = row.names(.))
pop_df <- fortify(shp3, region = "id")
pop_df <- pop_df %>% left_join(shp3@data, by = c("id"="id"))
pop_df <- as.data.frame(pop_df)
pop_df[,ncol(pop_df)][pop_df[,ncol(pop_df)] == 0] <- NA
colnames(pop_df)[ncol(pop_df)] <- "pop.val"
rate_df <- merge(shp_df,pop_df,by='order')
rate_df <- as.data.frame(rate_df)
rate_df$rate <- NA
rate_df$rate <- (rate_df$hia.val*100000)/rate_df$pop.val
#rate_df$rate[rate_df$rate==0]<-NA
rate.min <- min(rate_df[,ncol(rate_df)],na.rm=TRUE)
rate.max <- max(rate_df[,ncol(rate_df)],na.rm=TRUE)
rate.med <- median(rate_df[,ncol(rate_df)],na.rm=TRUE)
rate.min.label <- round(rate.min,2)
rate.max.label <- round(rate.max,2)
rate.med.label <- round(rate.med,2)
rate.mean <- (rate.min+rate.max)/2
rate.mean.label <- round(rate.mean,2)
write.csv(rate_df, paste(names(clip.groups[m]),',',outcome.groups[i],',',names(beta.groups[i]),',',names(conc.groups[j]),',',pop.names,',',rate.names,'cbg.results.csv'))
if((i == 1 | i == 4 | i == 7 | i == 10 | i == 13)&(j==1)){
# Map of excess per grid cell
e <- autoplot(base) +
geom_polygon(data = shp_df, aes(x = long, y = lat, group = group, fill = shp_df[,ncol(shp_df)]),alpha=0.7)+
scale_fill_gradient2("Count (n) cases \n per Census Block Group",
low = "#3ec267",
mid = "#fff429", #ff7e29
high = "#fc0339", ##ff1f40
midpoint = r.mean,
na.value='grey50',
breaks=c(r.min,r.mean,r.max),
labels=c(r.min.label,r.mean.label,r.max.label),
limits=c(r.min, r.max),
guide = guide_colourbar(
direction = "horizontal",
label=TRUE,
keyheight = unit(2, units = "mm"),
title.position = 'top',
title.hjust = 0.5,
label.hjust = 0.5,
barwidth = 15,
nrow = 1,
byrow = T,
label.position = "bottom"))+
theme_map()+
geom_path(data = cbg.shp.f, aes(x = long, y = lat, group = group),
color = "grey60", size = 0.1)+
labs(title=paste0(outcome.groups[i],' cases attributable to black carbon.',sep=''),
caption=paste0(names(beta.groups[i]),', ',names(conc.groups[j]),', ',rate.names,', \n',pop.names,'.',sep=''),
subtitle=paste0('Range: ',r.min.label,' to ',r.max.label,' per CBG. '),sep='')
ggsave(paste0(names(clip.groups[m]),' PAF ',outcome.groups[i],' ',names(beta.groups[i]),' ',names(conc.groups[j]),' ',pop.names,' ',rate.names,'.count.cbg.png',sep=''),dpi=320)
print('count.cbg')
autoplot(base) +
geom_polygon(data = rate_df, aes(x = long.x, y = lat.x, group = group.x, fill = rate_df$rate),alpha=0.7)+
scale_fill_gradient2("Rate per 100,000\nper Census Block Group",
low = "#3ec267",
mid = "#fff429", #ff7e29
high = "#fc0339", ##ff1f40
midpoint = rate.mean,
na.value='grey50',
breaks=c(rate.min,rate.mean,rate.max),
labels=c(rate.min.label,rate.mean.label,rate.max.label),
limits=c(rate.min, rate.max),
guide = guide_colourbar(
direction = "horizontal",
label=TRUE,
keyheight = unit(2, units = "mm"),
title.position = 'top',
title.hjust = 0.5,
label.hjust = 0.5,
barwidth = 15,
nrow = 1,
byrow = T,
label.position = "bottom"))+
theme_map()+
geom_path(data = cbg.shp.f, aes(x = long, y = lat, group = group),
color = "grey60", size = 0.1)+
labs(title=paste0(outcome.groups[i],' cases attributable to black carbon.',sep=''),
caption=paste0(names(beta.groups[i]),', ',names(conc.groups[j]),', ',rate.names,', \n',pop.names,'.',sep=''),
subtitle=paste0('Range: ',rate.min.label,' to ',rate.max.label,' per 100,000. '),sep='')
ggsave(paste0(names(clip.groups[m]),' PAF ',outcome.groups[i],' ',names(beta.groups[i]),' ',names(conc.groups[j]),' ',pop.names,' ',rate.names,'.rate.cbg.png',sep=''),dpi=320)
print('rate.cbg')
}
#/////////////////////////////////////////////////////////////////////////////////////////////
# City aggregation
if(m == 1 | m == 2 | m == 3){}
else{
city.shp <- readOGR(dsn=shps, layer=paste(city.groups))
crs(city.shp) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
city.shp <- crop(city.shp, shp)
city.shp.f <- fortify(city.shp) %>%
mutate(id = as.numeric(id))
zone.in <- city.shp
raster.in <- hia
shp2 <- ZonalPipe(zone.in, raster.in, stat="sum")
shp2@data <- shp2@data %>% mutate(id = row.names(.))
shp_df <- fortify(shp2, region = "id")
shp_df <- shp_df %>% left_join(shp2@data, by = c("id"="id"))
shp_df <- as.data.frame(shp_df)
shp_df[,ncol(shp_df)][shp_df[,ncol(shp_df)] == 0] <- NA
r.min <- min(shp_df[,ncol(shp_df)],na.rm=TRUE)
r.max <- max(shp_df[,ncol(shp_df)],na.rm=TRUE)
r.med <- median(shp_df[,ncol(shp_df)],na.rm=TRUE)
colnames(shp_df)[ncol(shp_df)] <- 'hia.val'
r.mean <- (r.min+r.max)/2
r.mean.label <- round(r.mean,2)
r.min.label <- round(r.min,2)
r.max.label <- round(r.max,2)
r.med.label <- round(r.med,2)
zone.in <- city.shp #======CHANGE=====#
raster.in <- c
shp3 <- ZonalPipe(zone.in, raster.in, stat="sum")
shp3@data <- shp3@data %>% mutate(id = row.names(.))
pop_df <- fortify(shp3, region = "id")
pop_df <- pop_df %>% left_join(shp3@data, by = c("id"="id"))
pop_df <- as.data.frame(pop_df)
pop_df[,ncol(pop_df)][pop_df[,ncol(pop_df)] == 0] <- NA
colnames(pop_df)[ncol(pop_df)] <- "pop.val"
rate_df <- merge(shp_df,pop_df,by='order')
rate_df <- as.data.frame(rate_df)
rate_df$rate <- NA
rate_df$rate <- (rate_df$hia.val*100000)/rate_df$pop.val
#rate_df$rate[rate_df$rate==0]<-NA
rate.min <- min(rate_df[,ncol(rate_df)],na.rm=TRUE)
rate.max <- max(rate_df[,ncol(rate_df)],na.rm=TRUE)
rate.med <- median(rate_df[,ncol(rate_df)],na.rm=TRUE)
rate.min.label <- round(rate.min,2)
rate.max.label <- round(rate.max,2)
rate.med.label <- round(rate.med,2)
rate.mean <- (rate.min+rate.max)/2
rate.mean.label <- round(rate.mean,2)
write.csv(rate_df, paste(names(clip.groups[m]),',',outcome.groups[i],',',names(beta.groups[i]),',',names(conc.groups[j]),',',pop.names,',',rate.names,'city.results.csv'))
}
#///////////////////////////////////////////////////////////////////////////
# County aggregation
if(m != 1 | m != 2 | m!=3){
co.groups <- c('bayco')
co.shp <- readOGR(dsn=shps, layer=paste(co.groups))
crs(co.shp) <- "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
co.shp <- crop(co.shp, shp)
co.shp.f <- fortify(co.shp) %>%
mutate(id = as.numeric(id))
zone.in <- co.shp
raster.in <- hia
shp2 <- ZonalPipe(zone.in, raster.in, stat="sum")
shp2@data <- shp2@data %>% mutate(id = row.names(.))
shp_df <- fortify(shp2, region = "id")
shp_df <- shp_df %>% left_join(shp2@data, by = c("id"="id"))
shp_df <- as.data.frame(shp_df)
shp_df[,ncol(shp_df)][shp_df[,ncol(shp_df)] == 0] <- NA
r.min <- min(shp_df[,ncol(shp_df)],na.rm=TRUE)
r.max <- max(shp_df[,ncol(shp_df)],na.rm=TRUE)
r.med <- median(shp_df[,ncol(shp_df)],na.rm=TRUE)
colnames(shp_df)[ncol(shp_df)] <- 'hia.val'
r.mean <- (r.min+r.max)/2
r.mean.label <- round(r.mean,2)
r.min.label <- round(r.min,2)
r.max.label <- round(r.max,2)
r.med.label <- round(r.med,2)
zone.in <- co.shp #======CHANGE=====#
raster.in <- c
shp3 <- ZonalPipe(zone.in, raster.in, stat="sum")
shp3@data <- shp3@data %>% mutate(id = row.names(.))
pop_df <- fortify(shp3, region = "id")
pop_df <- pop_df %>% left_join(shp3@data, by = c("id"="id"))
pop_df <- as.data.frame(pop_df)
pop_df[,ncol(pop_df)][pop_df[,ncol(pop_df)] == 0] <- NA
colnames(pop_df)[ncol(pop_df)] <- "pop.val"
rate_df <- merge(shp_df,pop_df,by='order')
rate_df <- as.data.frame(rate_df)
rate_df$rate <- NA
rate_df$rate <- (rate_df$hia.val*100000)/rate_df$pop.val
#rate_df$rate[rate_df$rate==0]<-NA
rate.min <- min(rate_df[,ncol(rate_df)],na.rm=TRUE)
rate.max <- max(rate_df[,ncol(rate_df)],na.rm=TRUE)
rate.med <- median(rate_df[,ncol(rate_df)],na.rm=TRUE)
rate.min.label <- round(rate.min,2)
rate.max.label <- round(rate.max,2)
rate.med.label <- round(rate.med,2)
rate.mean <- (rate.min+rate.max)/2
rate.mean.label <- round(rate.mean,2)
write.csv(rate_df, paste(names(clip.groups[m]),',',outcome.groups[i],',',names(beta.groups[i]),',',names(conc.groups[j]),',',pop.names,',',rate.names,'county.results.csv'))
rm(co.shp)
}
}
rm(shp)
rm(cbg.shp)
rm(c)
rm(hia)
rm(b)
rm(mr)
rm(af)
rm(af2)
rm(a)
}
}