-
Notifications
You must be signed in to change notification settings - Fork 0
/
Figures2020_ordered.R
2197 lines (1971 loc) · 104 KB
/
Figures2020_ordered.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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Figures of 2020 gas leaks analysis for comparison
# load packages and data
library(tidyverse)
library(sf)
library(tigris)
library(tmap)
library(tmaptools)
library(ggplot2)
library(foreign) # for reading in dbf
# library(tidytext) # for reorder_within and scale_x_reorder in ggplot2 facets
library(kableExtra)
# library(sp)
# library(spdep)
library(tigris)
# library(bibtex)
# Load demographic and gas leaks data
load("Data/Demographics.rds")
load("Data/HEET2020Leaks.rds")
ma_blkgrps <- readRDS("Data/ma_blkgrps2020.Rds")
ma_tracts <- readRDS("Data/ma_tracts2020.Rds")
ma_cosub <- readRDS("Data/ma_cosub2020.Rds")
ppLeakDensity_df_bg <- readRDS("Data/ppLeakDensity_df_blkgrps2020.Rds")
ppLeakDensity_df_tr <- readRDS("Data/ppLeakDensity_df_tracts2020.Rds")
ppLeakDensity_df_co <- readRDS("Data/ppLeakDensity_df_cosubs2020.Rds")
ppLeakDensityJoinedU_bg <- readRDS("Data/ppLeakDensityJoinedU_BG2020.Rds")
ppLeakDensityJoinedU_tr <- readRDS("Data/ppLeakDensityJoinedU_TR2020.Rds")
ppLeakDensityJoinedU_co <- readRDS("Data/ppLeakDensityJoinedU_CO2020.Rds")
# Harmonize CRS AND utility names to be same as 2019 data for easier processing
repaired2020 <- st_transform(repaired2020, crs = st_crs(ma_blkgrps)) %>%
mutate(Utility = recode(Utility, "Liberty utilities" = "Liberty Utilities",
"Eversource (NSTAR)" = "Eversource"))
unrepaired2020 <- st_transform(unrepaired2020, crs = st_crs(ma_blkgrps)) %>%
mutate(Utility = recode(Utility, "Liberty utilities" = "Liberty Utilities",
"Eversource (NSTAR)" = "Eversource"))
# Set up recurring map elements
# Load natural gas utility service areas dbf from MassGIS and join to MassGIS towns layer
ng_dbf <- read.dbf("Data/pubutil/TOWNS_POLY_UTILITIES.dbf")
ng_service_areas <- st_read(dsn = "Data/townssurvey_shp",
layer = "TOWNSSURVEY_POLYM") %>%
select(-TOWN) %>%
left_join(., ng_dbf, by = "TOWN_ID") %>%
select(TOWN, GAS, GAS_LABEL) %>%
st_transform(., crs = st_crs(ma_blkgrps)) %>%
st_make_valid()
# isolate No gas areas and municipal
ng_nogas_muni <- ng_service_areas %>%
filter(GAS_LABEL %in% c("No Natural Gas Service","Municipal")) %>%
group_by(GAS_LABEL) %>%
summarize() %>%
mutate(GAS_LABEL = as.character(GAS_LABEL))
# consolidate into basic gas service districts
ng_service_areas2 <- ng_service_areas %>%
filter(!GAS_LABEL %in% c("No Natural Gas Service","Municipal")) %>%
group_by(GAS_LABEL) %>%
summarize(TownCnt = n()) %>%
mutate(ID = case_when(
GAS_LABEL == "National Grid" ~ "NG",
GAS_LABEL == "Blackstone Gas Company" ~ "BGC",
GAS_LABEL == "Columbia Gas" ~ "EG",
GAS_LABEL == "Eversource Energy" ~ "EV",
GAS_LABEL == "Columbia Gas, Eversource Energy" ~ "EG,EV",
GAS_LABEL == "Unitil" ~ "UN",
GAS_LABEL == "National Grid, Unitil" ~ "NG,UN",
GAS_LABEL == "Eversource Energy, National Grid" ~ "EV,NG",
GAS_LABEL == "The Berkshire Gas Company" ~ "BG",
GAS_LABEL == "Columbia Gas, Blackstone Gas Company" ~ "EG,BGC",
GAS_LABEL == "Liberty Utilities" ~ "LU",
GAS_LABEL == "Colonial Gas" ~ "NG",
GAS_LABEL == "Columbia Gas, National Grid" ~ "EG,NG"
))
# join ID to original municipalities within ng_service_area to use centroids as labels; restrict to central municipalities within territories
ng_service_labels <- ng_service_areas2 %>%
as.data.frame() %>%
select(ID, GAS_LABEL) %>%
inner_join(ng_service_areas, ., by = "GAS_LABEL") %>%
filter(TOWN %in% c("LANESBOROUGH","DEERFIELD","NORTHAMPTON","LUDLOW",
"NORTH BROOKFIELD","OXFORD",
"WESTMINSTER","CARLISLE","ANDOVER","WESTBOROUGH",
"BLACKSTONE","MILTON",
"NORWOOD","WEST BRIDGEWATER","PLAINVILLE","PLYMOUTH",
"WESTPORT","ACUSHNET","WAREHAM","BARNSTABLE",
"SWANSEA","MARSHFIELD","COHASSET",
"HAMILTON","WEST NEWBURY","DOVER","DUNSTABLE")) %>%
st_centroid(., of_largest_polygon = TRUE)
ng_service_labels2 <- ng_service_areas2 %>%
as.data.frame() %>%
select(ID, GAS_LABEL) %>%
inner_join(ng_service_areas, ., by = "GAS_LABEL") %>%
filter(TOWN %in% c("LEICESTER","LUNENBURG","MENDON","BELLINGHAM","HANSON",
"WAYLAND","NATICK","BOSTON","SOMERVILLE")) %>%
st_centroid(., of_largest_polygon = TRUE)
ng_service_labels3 <- ng_service_areas2 %>%
as.data.frame() %>%
select(ID, GAS_LABEL) %>%
inner_join(ng_service_areas, ., by = "GAS_LABEL") %>%
filter(TOWN == "BOSTON") %>%
st_centroid(., of_largest_polygon = TRUE)
# separate MA for cropping
ma_state_sf <- states(cb = TRUE) %>%
filter(STUSPS == "MA")
# grab municipal boundaries
ma_towns_sf <- county_subdivisions(state = "MA", cb = TRUE) %>%
st_transform(., crs = 26986)
# create point layer of towns for context
ma_towns_sf_pts <- county_subdivisions(state = "MA", cb = TRUE) %>%
filter(NAME %in% c("Boston",
"Lawrence",
"Lowell",
"Brockton",
"Worcester",
"Springfield",
"Pittsfield",
"Stockbridge",
"Fall River",
"West Yarmouth",
"Lynn",
"Randolph",
"Webster",
"Attleboro",
"Medford",
"Amherst",
"Quincy",
"Weymouth Town",
"Nantucket")) %>%
mutate(NAME = recode(NAME, "Weymouth Town" = "Weymouth")) %>%
st_transform(., crs = 26986) %>%
st_centroid(of_largest_polygon = TRUE)
# create a separate point for Newton so that it can be repositioned
newton <- county_subdivisions(state = "MA", cb = TRUE) %>%
filter(NAME %in% c("Fitchburg","Newton","Edgartown","New Bedford")) %>%
st_transform(., crs = 26986) %>%
st_centroid(of_largest_polygon = TRUE)
# table of leak stats by class and repair status
unrepaired_Sumdf <- unrepaired2020 %>% as.data.frame() %>%
group_by(Class) %>% summarize(Unrepaired = n())
repaired_Sumdf <- repaired2020 %>% as.data.frame() %>%
group_by(Class) %>% summarize(Repaired = n())
total_Sumdf <- left_join(unrepaired_Sumdf, repaired_Sumdf, by = "Class") %>%
drop_na() %>%
mutate(Total = Unrepaired + Repaired) %>%
bind_rows(summarize(.,
across(where(is.numeric), sum),
across(where(is.character), ~ "Total"))) %>%
mutate(PctUnrepaired = Unrepaired/Total*100,
PctRepaired = Repaired/Total*100,
PctTotal = Total/Total[Class == "Total"]*100) %>%
select(Class, Unrepaired, PctUnrepaired, Repaired, PctRepaired,
Total, PctTotal)
total_Sumdf %>%
mutate(PctUnrepaired = paste0(round(PctUnrepaired,1),"%"),
PctRepaired = paste0(round(PctRepaired,1),"%"),
PctTotal = paste0(round(PctTotal,1),"%")) %>%
kable(., longtable = T, booktabs = T,
format.args = list(big.mark = ','),
caption = "Gas leaks by class and repair status, 2020", align = "r",
# digits = c(0,0,1,0,1,0,1),
col.names = c("Class","Count","Percent","Count","Percent",
"Total","Pct Total")) %>%
add_header_above(., c(" ", "Unrepaired Leaks" = 2, "Repaired Leaks" = 2,
" " = 2)) %>%
row_spec(4, bold = T) %>%
kable_styling(latex_options = c("repeat_header"))
# Map of leak counts by hexagons and utility territories
# create a hexagonal grid and create index column
gridCnt <- st_make_grid(x = ma_blkgrps, cellsize = 1000, square = FALSE) %>%
st_as_sf() %>%
mutate(index = row_number())
# spatially join to grid ids to leaks, sum aggregate numbers of leaks per grid index, and then join sums back to hexagons for mapping and analysis
gridCnt <- unrepaired2020 %>%
st_join(., gridCnt) %>%
st_drop_geometry() %>%
group_by(index) %>%
summarize(unrepaired = n()) %>%
left_join(gridCnt, ., by = "index") %>%
replace_na(list(unrepaired = 0))
gridCnt <- repaired2020 %>%
st_join(., gridCnt) %>%
st_drop_geometry() %>%
group_by(index) %>%
summarize(repaired = n()) %>%
left_join(gridCnt, ., by = "index") %>%
replace_na(list(repaired = 0))
# create column with total leak points and clip to MA
gridCnt <- gridCnt %>%
mutate(total = unrepaired + repaired) %>%
crop_shape(., ma_blkgrps, polygon = TRUE) %>%
st_make_valid()
gridCntT <- gridCnt %>%
filter(total > 0)
m_gridTotal <- tm_shape(ng_nogas_muni, bbox = ma_state_sf) +
tm_fill(col = "GAS_LABEL", palette = c("honeydew2","gray88"), title = "") +
tm_shape(gridCntT, unit = "km", bbox = ma_state_sf) +
tm_fill(col = "total", palette = "YlOrRd",
style = "fisher",
# breaks = c(1,5,10,20,40,80),
legend.format = list(digits = 0),
title = "Number\nof leaks") +
tm_shape(ng_service_areas2) + tm_borders(lwd = 0.8, col = "grey68", alpha = 0.7) +
tm_shape(ng_service_labels) +
tm_text("ID", size = 0.6, col = "gray") +
tm_shape(ng_service_labels2) +
tm_text("ID", size = 0.4, col = "gray") +
tm_shape(ng_service_labels3) +
tm_text("ID", size = 0.6, col = "gray", xmod = 2.7, ymod = 0.7) +
# tm_shape(EJlayer) + tm_borders(col = "blue", alpha = 0.7) +
tm_shape(ma_towns_sf_pts) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = 0.7, ymod = 0.2, shadow = TRUE) +
tm_shape(newton) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = -0.7, ymod = 0.2, shadow = TRUE) +
tm_scale_bar(breaks = c(0,25,50), position = c("center","BOTTOM")) +
tm_layout(legend.position = c("left","bottom"),
title = "Massachusetts Gas Leaks and Utility Territories, 2020",
inner.margins = c(0.02,0.02,0.09,0.02)
) +
tm_credits("BG = Berkshire Gas\nBGC = Blackstone Gas Co.\nEG = Eversource Gas Co. of MA\nEV = Eversource Energy\nLU = Liberty Utilities\nNG = National Grid\nUN = Unitil/Fitchburg Gas", position = c(.3,.08), col = "gray47", size = 0.7)
tmap_save(m_gridTotal, filename = "Images/pub/m_gridTotalUtil2020.png",
dpi = 600, height = 4, width = 8, units = "in")
knitr::include_graphics("Images/pub/m_gridTotalUtil2020.png")
# create map of leak densities by BG
blkgrpsLeaksAll <- ma_blkgrps %>%
filter(AllLeaks2020_sqkm > 0)
m1 <- tm_shape(ng_nogas_muni, bbox = ma_state_sf) +
tm_fill(col = "GAS_LABEL", palette = c("honeydew2","gray88"), title = "") +
tm_shape(blkgrpsLeaksAll) + tm_fill(col = "AllLeaks2020_sqkm", style = "fisher",
palette = "YlOrRd",
title = "Leaks per SqKm",
legend.format = list(digits = 2)) +
tm_shape(ma_blkgrps) + tm_borders(lwd = 0.01, alpha = 0.8) +
tm_shape(ng_service_areas2) + tm_borders(lwd = 0.8, col = "grey68", alpha = 0.7) +
tm_shape(ng_service_labels) +
tm_text("ID", size = 0.6, col = "gray") +
tm_shape(ng_service_labels2) +
tm_text("ID", size = 0.4, col = "gray") +
tm_shape(ng_service_labels3) +
tm_text("ID", size = 0.6, col = "gray", xmod = 2.7, ymod = 0.7) +
# tm_shape(EJlayer) + tm_borders(col = "blue", alpha = 0.7) +
tm_shape(ma_towns_sf_pts) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = 0.7, ymod = 0.2, shadow = TRUE) +
tm_shape(newton) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = -0.7, ymod = 0.2, shadow = TRUE) +
tm_scale_bar(breaks = c(0,25,50), position = c("center","BOTTOM")) +
tm_layout(legend.position = c("left","bottom"),
title = "Massachusetts Gas Leak Density by Census Block Group and Utility Territories, 2020",
inner.margins = c(0.02,0.02,0.09,0.02)) +
tm_credits("BG = Berkshire Gas\nBGC = Blackstone Gas Co.\nEG = Eversource Gas Co. of MA\nEV = Eversource Energy\nLU = Liberty Utilities\nNG = National Grid\nUN = Unitil/Fitchburg Gas", position = c(.3,.08), col = "gray47", size = 0.7)
tmap_save(m1, filename = "Images/pub/m_blkgrpTotal2020.png",
dpi = 600, height = 4, width = 8, units = "in")
knitr::include_graphics("Images/pub/m_blkgrpTotal2020.png")
# map of leaks by tract
tractsLeaksAll <- ma_tracts %>%
filter(AllLeaks2020_sqkm > 0)
m2 <- tm_shape(ng_nogas_muni, bbox = ma_state_sf) +
tm_fill(col = "GAS_LABEL", palette = c("honeydew2","gray88"), title = "") +
tm_shape(tractsLeaksAll) + tm_fill(col = "AllLeaks2020_sqkm", style = "fisher",
palette = "YlOrRd",
title = "Leaks per SqKm",
legend.format = list(digits = 2)) +
tm_shape(ma_tracts) + tm_borders(lwd = 0.01, alpha = 0.8) +
tm_shape(ng_service_areas2) + tm_borders(lwd = 0.8, col = "grey68", alpha = 0.7) +
tm_shape(ng_service_labels) +
tm_text("ID", size = 0.6, col = "gray") +
tm_shape(ng_service_labels2) +
tm_text("ID", size = 0.4, col = "gray") +
tm_shape(ng_service_labels3) +
tm_text("ID", size = 0.6, col = "gray", xmod = 2.7, ymod = 0.7) +
# tm_shape(EJlayer) + tm_borders(col = "blue", alpha = 0.7) +
tm_shape(ma_towns_sf_pts) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = 0.7, ymod = 0.2, shadow = TRUE) +
tm_shape(newton) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = -0.7, ymod = 0.2, shadow = TRUE) +
tm_scale_bar(breaks = c(0,25,50), position = c("center","BOTTOM")) +
tm_layout(legend.position = c("left","bottom"),
title = "Massachusetts Gas Leak Density by Census Tract and Utility Territories, 2020",
inner.margins = c(0.02,0.02,0.09,0.02)) +
tm_credits("BG = Berkshire Gas\nBGC = Blackstone Gas Co.\nEG = Eversource Gas Co. of MA\nEV = Eversource Energy\nLU = Liberty Utilities\nNG = National Grid\nUN = Unitil/Fitchburg Gas", position = c(.3,.08), col = "gray47", size = 0.7)
tmap_save(m2, filename = "Images/pub/m_tractTotal2020.png",
dpi = 600, height = 4, width = 8, units = "in")
knitr::include_graphics("Images/pub/m_tractTotal2020.png")
# map by municipality
cosubLeaksAll <- ma_cosub %>%
filter(AllLeaks2020_sqkm > 0)
m3 <- tm_shape(ng_nogas_muni, bbox = ma_state_sf) +
tm_fill(col = "GAS_LABEL", palette = c("honeydew2","gray88"), title = "") +
tm_shape(cosubLeaksAll) + tm_fill(col = "AllLeaks2020_sqkm", style = "fisher",
palette = "YlOrRd",
title = "Leaks per SqKm",
legend.format = list(digits = 2)) +
tm_shape(ma_cosub) + tm_borders(lwd = 0.01, alpha = 0.8) +
tm_shape(ng_service_areas2) + tm_borders(lwd = 0.8, col = "grey68", alpha = 0.7) +
tm_shape(ng_service_labels) +
tm_text("ID", size = 0.6, col = "gray") +
tm_shape(ng_service_labels2) +
tm_text("ID", size = 0.4, col = "gray") +
tm_shape(ng_service_labels3) +
tm_text("ID", size = 0.6, col = "gray", xmod = 2.7, ymod = 0.7) +
# tm_shape(EJlayer) + tm_borders(col = "blue", alpha = 0.7) +
tm_shape(ma_towns_sf_pts) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = 0.7, ymod = 0.2, shadow = TRUE) +
tm_shape(newton) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = -0.7, ymod = 0.2, shadow = TRUE) +
tm_scale_bar(breaks = c(0,25,50), position = c("center","BOTTOM")) +
tm_layout(legend.position = c("left","bottom"),
title = "Massachusetts Gas Leak Density by Municipality and Utility Territories, 2020",
inner.margins = c(0.02,0.02,0.09,0.02)) +
tm_credits("BG = Berkshire Gas\nBGC = Blackstone Gas Co.\nEG = Eversource Gas Co. of MA\nEV = Eversource Energy\nLU = Liberty Utilities\nNG = National Grid\nUN = Unitil/Fitchburg Gas", position = c(.3,.08), col = "gray47", size = 0.7)
tmap_save(m3, filename = "Images/pub/m_cosubTotal2020.png", dpi = 600, height = 4,
width = 8, units = "in")
knitr::include_graphics("Images/pub/m_cosubTotal2020.png")
# table of leak count stats by block group
# list of stats to compute across
summary_stats <- list(Min = ~min(., na.rm = T),
Med = ~median(., na.rm = T),
Avg = ~mean(., na.rm = T),
Max = ~max(., na.rm = T))
# table of stats by block group
unrep_blkgrp_cnt <- ma_blkgrps %>%
as.data.frame() %>%
select(starts_with("unrepaired")) %>%
pivot_longer(cols = everything(), names_to = "Class", values_to = "Count") %>%
group_by(Class) %>%
summarize(across(.cols = Count, summary_stats,
.names = "{.fn}U")) %>%
mutate(Class = recode(Class, "unrepaired2020total" = "All",
"unrepaired2020totalC1" = "1",
"unrepaired2020totalC2" = "2",
"unrepaired2020totalC3" = "3")) %>%
arrange(Class)
rep_blkgrp_cnt <- ma_blkgrps %>%
as.data.frame() %>%
select(starts_with("repaired")) %>%
pivot_longer(cols = everything(), names_to = "Class", values_to = "Count") %>%
group_by(Class) %>%
summarize(across(.cols = Count, summary_stats,
.names = "{.fn}R")) %>%
mutate(Class = recode(Class, "repaired2020total" = "All",
"repaired2020totalC1" = "1",
"repaired2020totalC2" = "2",
"repaired2020totalC3" = "3")) %>%
arrange(Class)
all_blkgrp_cnt <- ma_blkgrps %>%
as.data.frame() %>%
select(AllLeaks2020:AllLeaks2020C3) %>%
pivot_longer(cols = everything(), names_to = "Class", values_to = "Count") %>%
group_by(Class) %>%
summarize(across(.cols = Count, summary_stats,
.names = "{.fn}T")) %>%
mutate(Class = recode(Class, "AllLeaks2020" = "All",
"AllLeaks2020C1" = "1",
"AllLeaks2020C2" = "2",
"AllLeaks2020C3" = "3")) %>%
arrange(Class)
# join together
list(unrep_blkgrp_cnt,rep_blkgrp_cnt,all_blkgrp_cnt) %>%
reduce(., left_join, by = "Class") %>%
kable(., longtable = T, booktabs = T,
# format.args = list(big.mark = ','),
caption = "Gas leak counts by class per Census Block Group, 2020",
align = "r",
digits = c(0,0,0,1,0,0,0,1,0,0,0,1),
col.names = c("Class","Min","Med","Avg","Max",
"Min","Med","Avg","Max",
"Min","Med","Avg","Max")) %>%
add_header_above(., c(" ", "Unrepaired Leaks" = 4, "Repaired Leaks" = 4,
"Total Leaks" = 4)) %>%
# row_spec(4, bold = T) %>%
kable_styling(latex_options = c("repeat_header"))
# table of leak density stats by block group
# list of stats to compute across
summary_stats <- list(Min = ~min(., na.rm = T),
Med = ~median(., na.rm = T),
Avg = ~mean(., na.rm = T),
Max = ~max(., na.rm = T))
# table of stats by block group
unrep_blkgrp_dns <- ma_blkgrps %>%
as.data.frame() %>%
select(starts_with("leaks_sqkm")) %>%
pivot_longer(cols = everything(), names_to = "Class", values_to = "Count") %>%
group_by(Class) %>%
summarize(across(.cols = Count, summary_stats,
.names = "{.fn}U")) %>%
mutate(Class = recode(Class, "leaks_sqkm" = "All",
"leaks_sqkmC1" = "1",
"leaks_sqkmC2" = "2",
"leaks_sqkmC3" = "3")) %>%
arrange(Class)
rep_blkgrp_dns <- ma_blkgrps %>%
as.data.frame() %>%
select(starts_with("REPleaks_sqkm")) %>%
pivot_longer(cols = everything(), names_to = "Class", values_to = "Count") %>%
group_by(Class) %>%
summarize(across(.cols = Count, summary_stats,
.names = "{.fn}R")) %>%
mutate(Class = recode(Class, "REPleaks_sqkm" = "All",
"REPleaks_sqkmC1" = "1",
"REPleaks_sqkmC2" = "2",
"REPleaks_sqkmC3" = "3")) %>%
arrange(Class)
all_blkgrp_dns <- ma_blkgrps %>%
as.data.frame() %>%
select(AllLeaks2020_sqkm:AllLeaks2020C3_sqkm) %>%
pivot_longer(cols = everything(), names_to = "Class", values_to = "Count") %>%
group_by(Class) %>%
summarize(across(.cols = Count, summary_stats,
.names = "{.fn}T")) %>%
mutate(Class = recode(Class, "AllLeaks2020_sqkm" = "All",
"AllLeaks2020C1_sqkm" = "1",
"AllLeaks2020C2_sqkm" = "2",
"AllLeaks2020C3_sqkm" = "3")) %>%
arrange(Class)
# join together
list(unrep_blkgrp_dns,rep_blkgrp_dns,all_blkgrp_dns) %>%
reduce(., left_join, by = "Class") %>%
kable(., longtable = T, booktabs = T,
# format.args = list(big.mark = ','),
caption = "Gas leak density (per sqkm) by class per Census Block Group, 2020",
align = "r",
digits = 2,
col.names = c("Class","Min","Med","Avg","Max",
"Min","Med","Avg","Max",
"Min","Med","Avg","Max")) %>%
add_header_above(., c(" ", "Unrepaired Leaks" = 4, "Repaired Leaks" = 4,
"Total Leaks" = 4)) %>%
# row_spec(4, bold = T) %>%
kable_styling(latex_options = c("repeat_header"))
# create a stacked bar chart of leaks by utility. shows significant difference in magnitude.
unrepairedUSum_df <- unrepaired2020 %>%
as.data.frame() %>%
mutate(Utility = recode(Utility,
"Fitchburg" = "Fitchburg Gas",
"Eversource" = "Eversource Energy")) %>%
group_by(Utility) %>%
summarize(Unrepaired = n())
repairedUSum_df <- repaired2020 %>%
as.data.frame() %>%
mutate(Utility = recode(Utility,
"Fitchburg" = "Fitchburg Gas",
"Eversource" = "Eversource Energy")) %>%
group_by(Utility) %>%
summarize(Repaired = n())
totalUSum_df <- full_join(unrepairedUSum_df, repairedUSum_df, by = "Utility") %>%
replace_na(list(Unrepaired = 0)) %>%
mutate(Total = Unrepaired + Repaired) %>%
bind_rows(summarize(.,
across(where(is.numeric), sum),
across(where(is.character), ~ "Total"))) %>%
mutate(PctUnrepaired = Unrepaired/Total*100,
PctRepaired = Repaired/Total*100,
PctTotal = Total/Total[Utility == "Total"]*100) %>%
select(Utility, Unrepaired, PctUnrepaired, Repaired, PctRepaired, Total, PctTotal)
totalUSum_df %>%
filter(Utility != "Total") %>%
select(-c(Total, starts_with("Pct"))) %>%
pivot_longer(., cols = c(Unrepaired,Repaired),
names_to = "status", values_to = "leaks") %>%
ggplot(aes(x = reorder(Utility, leaks), y = leaks,
fill = status)) +
geom_bar(position = "stack", stat = "identity") +
scale_fill_manual("legend", values = c("Repaired" = "#7CB5EC",
"Unrepaired" = "#F7A35C")) +
coord_flip() +
theme_minimal() +
theme(legend.title = element_blank(), legend.position = "top") +
scale_y_continuous(labels=function(x) format(x, big.mark = ",", scientific = FALSE)) +
guides(fill = guide_legend(reverse = TRUE)) +
labs(x = NULL,
y = "Reported Leaks",
title = "Reported leaks by utility in 2020 across Massachusetts")
ggsave("Images/pub/LeaksbyUtility2020.png")
# dot graph of relative exposure for unrepaired leak density around zero line
cols <- c("#F7A35C", "#7CB5EC", "gray30")
shps <- c(0,2,1)
# create a consistent factor order for groups using order from 2019 for year-to-year comparison
load("Images/pub/factorOrder2019.Rds")
# group_orderBG <- ppLeakDensity_df_bg %>%
# arrange(desc(wLeaksRR)) %>%
# select(Group) %>%
# pull()
#
# group_orderTR <- ppLeakDensity_df_tr %>%
# arrange(wLeaksRR) %>%
# select(Group) %>%
# pull()
#
# group_orderCO <- ppLeakDensity_df_co %>%
# arrange(wLeaksRR) %>%
# select(Group) %>%
# pull()
# Set up for dot graph to show all three scales
ppLeakDensity_df_bg2 <- ppLeakDensity_df_bg %>%
mutate(Group = factor(Group, levels = group_orderBG)) %>%
pivot_longer(wLeaksRR:wLeaksRRC3,
names_to = "leakClass", values_to = "leakDensityBG") %>%
# filter(!Group %in% c("MA_ENGLISH","MA_MINORITY21","MA_INCOME21")) %>%
mutate(Group = recode(Group, "MA_ENGLISH" = "MA Limited English HH",
"MA_MINORITY21" = "MA Minority",
"MA_INCOME21" = "MA Low Income")) %>%
mutate(leakClass = recode(leakClass, "wLeaksRR" = "All Leaks",
"wLeaksRRC1" = "Class 1 Leaks (high hazard)",
"wLeaksRRC2" = "Class 2 Leaks (med hazard)",
"wLeaksRRC3" = "Class 3 Leaks (low hazard)"),
# Group = reorder_within(Group, leakDensityBG, leakClass),
Status = case_when(
leakDensityBG == 1 ~ "Pop Mean",
leakDensityBG > 1 ~ "Greater Exposure",
leakDensityBG < 1 ~ "Lower Exposure"
),
Scale = "Block Group") %>%
select(Group, leakDensityBG, leakClass, Status, Scale)
# Extract rows with EJ groups to rbind with other dfs
EJrowsTR <- ppLeakDensity_df_bg2 %>%
filter(str_detect(Group, "MA ")) %>%
mutate(leakDensityBG = NA,
Scale = "Tract") %>%
rename(leakDensityTR = leakDensityBG)
EJrowsCO <- ppLeakDensity_df_bg2 %>%
filter(str_detect(Group, "MA ")) %>%
mutate(leakDensityBG = NA,
Scale = "Municipality") %>%
rename(leakDensityCO = leakDensityBG)
# repeat for tracts
ppLeakDensity_df_tr2 <- ppLeakDensity_df_tr %>%
mutate(Group = factor(Group, levels = group_orderTR)) %>%
pivot_longer(wLeaksRR:wLeaksRRC3,
names_to = "leakClass", values_to = "leakDensityTR") %>%
# filter(!Group %in% c("Disabled Adults", "Housing Burdened")) %>%
mutate(leakClass = recode(leakClass, "wLeaksRR" = "All Leaks",
"wLeaksRRC1" = "Class 1 Leaks (high hazard)",
"wLeaksRRC2" = "Class 2 Leaks (med hazard)",
"wLeaksRRC3" = "Class 3 Leaks (low hazard)"),
# Group = reorder_within(Group, leakDensityTR, leakClass),
Status = case_when(
leakDensityTR == 1 ~ "Pop Mean",
leakDensityTR > 1 ~ "Greater Exposure",
leakDensityTR < 1 ~ "Lower Exposure"
),
Scale = "Tract") %>%
select(Group, leakDensityTR, leakClass, Status, Scale) %>%
rbind(EJrowsTR)
# repeat for cosubs
ppLeakDensity_df_co2 <- ppLeakDensity_df_co %>%
mutate(Group = factor(Group, levels = group_orderCO)) %>%
pivot_longer(wLeaksRR:wLeaksRRC3,
names_to = "leakClass", values_to = "leakDensityCO") %>%
# filter(!Group %in% c("Disabled Adults", "Housing Burdened")) %>%
mutate(leakClass = recode(leakClass, "wLeaksRR" = "All Leaks",
"wLeaksRRC1" = "Class 1 Leaks (high hazard)",
"wLeaksRRC2" = "Class 2 Leaks (med hazard)",
"wLeaksRRC3" = "Class 3 Leaks (low hazard)"),
# Group = reorder_within(Group, leakDensityCO, leakClass),
Status = case_when(
leakDensityCO == 1 ~ "Pop Mean",
leakDensityCO > 1 ~ "Greater Exposure",
leakDensityCO < 1 ~ "Lower Exposure"
),
Scale = "Municipality") %>%
select(Group, leakDensityCO, leakClass, Status, Scale) %>%
rbind(EJrowsCO)
# Extract rows with Disabled and HBurdened to rbind with BG df
ppLeakDensity_df_bg2 <- ppLeakDensity_df_co2 %>%
filter(str_detect(Group, "Disabled|Housing")) %>%
mutate(leakDensityCO = NA,
Scale = "Block Group") %>%
rename(leakDensityBG = leakDensityCO) %>%
rbind(ppLeakDensity_df_bg2,.)
# join them all together
ppLeakDensity_df_all <- list(ppLeakDensity_df_tr2, ppLeakDensity_df_bg2,
ppLeakDensity_df_co2) %>%
lapply(., function(x) mutate(x, ClassGroup = paste(leakClass,Group))) %>%
reduce(., full_join, by = "ClassGroup") %>%
rowwise() %>%
mutate(min = min(leakDensityBG, leakDensityTR, leakDensityCO, na.rm = T),
med = median(leakDensityBG, leakDensityTR, leakDensityCO, na.rm = T),
max = max(leakDensityBG, leakDensityTR, leakDensityCO, na.rm = T),
Status = case_when(
min > 1 & max > 1 ~ "Greater Exposure",
min < 1 & max < 1 ~ "Lower Exposure",
min < 1 & max > 1 ~ "Pop Mean",
min > 1 & max < 1 ~ "Pop Mean")) %>%
select(-(ends_with(".x")|ends_with(".y")))
# create an alternate df_all with rbind so that we can map aesthetic to shape
ppLeakDensity_df_all2 <- list(ppLeakDensity_df_tr2, ppLeakDensity_df_bg2,
ppLeakDensity_df_co2) %>%
lapply(., function(x) rename(x,leakDensity = starts_with("leakDensity"))) %>%
reduce(., rbind)
# create the figure with shapes for each scale
ppLeakDensity_df_all2 %>%
mutate(Scale = factor(Scale, levels = c("Block Group", "Tract",
"Municipality"))) %>%
ggplot(aes(x = Group, y = leakDensity, color = Status, shape = Scale)) +
geom_point() +
geom_hline(yintercept = 1, color = "gray30") +
scale_color_manual(values = cols, guide = "legend") +
scale_shape_manual(values = shps, guide = "legend") +
geom_linerange(data = ppLeakDensity_df_all,
aes(x = Group, y = med, ymin = min, ymax = max), size = 0.3) +
# coord_cartesian(ylim = c(0.5,3.5), expand = TRUE) +
scale_y_continuous(limits = c(0.5,3.5)) +
coord_flip() +
# scale_x_reordered() +
theme_minimal(base_size = 6) +
theme(legend.title=element_blank(), legend.position = "top",
plot.caption = element_text(hjust = 0)) +
facet_wrap(~ leakClass, scales = "free_y") +
labs(x = NULL,
y = expression(paste("Ratio of group population-weighted mean leak density (leaks/",
km^2, ")", " to total population-weighted mean",sep = "")),
title = "Relative Exposure to Unrepaired Gas Leaks in 2020 across Massachusetts", caption = "Colors indicate if exposure is above (orange), below (blue), or equal to (gray) general population. Shapes indicate scale or unit of analysis. Colors of horizontal bars through shapes indicate if exposure is consistently\nabove (orange) or below (blue), or if it is mixed (gray). Environmental Justice communities ('MA') only evaluated at Block Group scale. Disabled Adults and Housing Burdened only evaluated at Tract and Municipality scales.")
ggsave("Images/pub/DotGraphallscalesRE2020ordered.png")
# Set up for dot graph to show all three scales by utility
ppLeakDensityJoinedU_bg2 <- ppLeakDensityJoinedU_bg %>%
mutate(Group = factor(Group, levels = group_orderBG)) %>%
pivot_longer(c(wLeaksRRBG, wLeaksRREG, wLeaksRREV,
wLeaksRRFG, wLeaksRRLU, wLeaksRRNG),
names_to = "Utility", values_to = "leakDensityBG") %>%
filter(!Group %in% c("Native American", "Other race",
"Native Pacific Islander", "Two or more races",
"MA_MINORITY17", "MA_INCOME17")) %>%
drop_na(leakDensityBG) %>%
mutate(Group = recode(Group, "MA_ENGLISH" = "MA Limited English HH",
"MA_MINORITY21" = "MA Minority",
"MA_INCOME21" = "MA Low Income"),
Utility = recode(Utility, "wLeaksRRBG" = "Berkshire Gas",
"wLeaksRREG" = "EGMA",
"wLeaksRREV" = "Eversource Energy",
"wLeaksRRLU" = "Liberty Utilities",
"wLeaksRRNG" = "National Grid",
"wLeaksRRFG" = "Unitil/Fitchburg Gas"),
# Group = reorder_within(Group, leakDensityBG, Utility),
Status = case_when(
leakDensityBG == 1 ~ "Pop Mean",
leakDensityBG > 1 ~ "Greater Exposure",
leakDensityBG < 1 ~ "Lower Exposure"
),
Scale = "Block Group") %>%
select(Group, leakDensityBG, Utility, Status, Scale)
# Extract rows with EJ groups to rbind with other dfs
EJrowsTR <- ppLeakDensityJoinedU_bg2 %>%
filter(str_detect(Group, "MA ")) %>%
mutate(leakDensityBG = NA,
Scale = "Tract") %>%
rename(leakDensityTR = leakDensityBG)
EJrowsCO <- ppLeakDensityJoinedU_bg2 %>%
filter(str_detect(Group, "MA ")) %>%
mutate(leakDensityBG = NA,
Scale = "Municipality") %>%
rename(leakDensityCO = leakDensityBG)
# repeat for tracts
ppLeakDensityJoinedU_tr2 <- ppLeakDensityJoinedU_tr %>%
mutate(Group = factor(Group, levels = group_orderTR)) %>%
pivot_longer(c(wLeaksRRBG, wLeaksRREG, wLeaksRREV,
wLeaksRRFG, wLeaksRRLU, wLeaksRRNG),
names_to = "Utility", values_to = "leakDensityTR") %>%
filter(!Group %in% c("Native American", "Other race",
"Native Pacific Islander", "Two or more races")) %>%
drop_na(leakDensityTR) %>%
mutate(Utility = recode(Utility, "wLeaksRRBG" = "Berkshire Gas",
"wLeaksRREG" = "EGMA",
"wLeaksRREV" = "Eversource Energy",
"wLeaksRRLU" = "Liberty Utilities",
"wLeaksRRNG" = "National Grid",
"wLeaksRRFG" = "Unitil/Fitchburg Gas"),
# Group = reorder_within(Group, leakDensityTR, Utility),
Status = case_when(
leakDensityTR == 1 ~ "Pop Mean",
leakDensityTR > 1 ~ "Greater Exposure",
leakDensityTR < 1 ~ "Lower Exposure"
),
Scale = "Tract") %>%
select(Group, leakDensityTR, Utility, Status, Scale) %>%
rbind(EJrowsTR)
# repeat for cosubs
ppLeakDensityJoinedU_co2 <- ppLeakDensityJoinedU_co %>%
mutate(Group = factor(Group, levels = group_orderCO)) %>%
pivot_longer(c(wLeaksRRBG, wLeaksRREG, wLeaksRREV,
wLeaksRRFG, wLeaksRRLU, wLeaksRRNG),
names_to = "Utility", values_to = "leakDensityCO") %>%
filter(!Group %in% c("Native American", "Other race",
"Native Pacific Islander", "Two or more races")) %>%
drop_na(leakDensityCO) %>%
mutate(Utility = recode(Utility, "wLeaksRRBG" = "Berkshire Gas",
"wLeaksRREG" = "EGMA",
"wLeaksRREV" = "Eversource Energy",
"wLeaksRRLU" = "Liberty Utilities",
"wLeaksRRNG" = "National Grid",
"wLeaksRRFG" = "Unitil/Fitchburg Gas"),
# Group = reorder_within(Group, leakDensityCO, Utility),
Status = case_when(
leakDensityCO == 1 ~ "Pop Mean",
leakDensityCO > 1 ~ "Greater Exposure",
leakDensityCO < 1 ~ "Lower Exposure"
),
Scale = "Municipality") %>%
select(Group, leakDensityCO, Utility, Status, Scale) %>%
rbind(EJrowsCO)
# Extract rows with Disabled and HBurdened to rbind with BG df
ppLeakDensityJoinedU_bg2 <- ppLeakDensityJoinedU_co2 %>%
filter(str_detect(Group, "Disabled|Housing")) %>%
mutate(leakDensityCO = NA,
Scale = "Block Group") %>%
rename(leakDensityBG = leakDensityCO) %>%
rbind(ppLeakDensityJoinedU_bg2,.)
# join them all together
ppLeakDensityJoinedU_all <- list(ppLeakDensityJoinedU_tr2,
ppLeakDensityJoinedU_bg2,
ppLeakDensityJoinedU_co2) %>%
lapply(., function(x) mutate(x, UtilityGroup = paste(Utility,Group))) %>%
reduce(., full_join, by = "UtilityGroup") %>%
rowwise() %>%
mutate(min = min(leakDensityBG, leakDensityTR, leakDensityCO, na.rm = T),
med = median(leakDensityBG, leakDensityTR, leakDensityCO, na.rm = T),
max = max(leakDensityBG, leakDensityTR, leakDensityCO, na.rm = T),
Status = case_when(
min > 1 & max > 1 ~ "Greater Exposure",
min < 1 & max < 1 ~ "Lower Exposure",
min < 1 & max > 1 ~ "Pop Mean",
min > 1 & max < 1 ~ "Pop Mean")) %>%
select(-(ends_with(".x")|ends_with(".y")))
# create an alternate df_all with rbind so that we can map aesthetic to shape
ppLeakDensityJoinedU_all2 <- list(ppLeakDensityJoinedU_tr2,
ppLeakDensityJoinedU_bg2,
ppLeakDensityJoinedU_co2) %>%
lapply(., function(x) rename(x,leakDensity = starts_with("leakDensity"))) %>%
reduce(., rbind)
# create the figure with shapes for each scale
cols <- c("#F7A35C", "#7CB5EC", "gray30")
shps <- c(0,2,1)
ppLeakDensityJoinedU_all2 %>%
mutate(Scale = factor(Scale, levels = c("Block Group", "Tract",
"Municipality"))) %>%
ggplot(aes(x = Group, y = leakDensity, color = Status, shape = Scale)) +
geom_point() +
geom_hline(yintercept = 1, color = "gray30") +
scale_color_manual(values = cols, guide = "legend") +
scale_shape_manual(values = shps, guide = "legend") +
geom_linerange(data = ppLeakDensityJoinedU_all,
aes(x = Group, y = med, ymin = min, ymax = max), size = 0.3) +
coord_flip() +
# scale_x_reordered() +
theme_minimal(base_size = 6) +
theme(legend.title=element_blank(), legend.position = "top",
plot.caption = element_text(hjust = 0)) +
facet_wrap(~ Utility, scales = "free_y") +
labs(x = NULL,
y = expression(paste("Ratio of group population-weighted mean leak density (leaks/",
km^2, ")", " to total population-weighted mean",sep = "")),
title = "Relative Exposure to Unrepaired Gas Leaks in 2020 across Massachusetts by Utility", caption = "Colors indicate if exposure is above (orange), below (blue), or equal to (gray) general population. Shapes indicate scale or unit of analysis. Colors of horizontal bars through shapes indicate if exposure is consistently\nabove (orange) or below (blue), or if it is mixed (gray). Environmental Justice communities ('MA') only evaluated at Block Group scale. Disabled Adults and Housing Burdened only evaluated at Tract and Municipality scales.")
ggsave("Images/pub/DotGraphallscalesUtilityRE2020ordered.png")
### Statewide exposure normalized by occupied housing units
# create map of leaks per OHU by BG
blkgrpsLeaksOHU <- ma_blkgrps %>%
filter(ALLleaks_hu > 0)
m1b <- tm_shape(ng_nogas_muni, bbox = ma_state_sf) +
tm_fill(col = "GAS_LABEL", palette = c("honeydew2","gray88"), title = "") +
tm_shape(blkgrpsLeaksOHU) + tm_fill(col = "ALLleaks_hu", style = "fisher",
palette = "YlOrRd",
title = "Leaks per Occupied\nHousing Unit",
legend.format = list(digits = 2)) +
tm_shape(ma_blkgrps) + tm_borders(lwd = 0.01, alpha = 0.8) +
tm_shape(ng_service_areas2) + tm_borders(lwd = 0.8, col = "grey68", alpha = 0.7) +
tm_shape(ng_service_labels) +
tm_text("ID", size = 0.6, col = "gray") +
tm_shape(ng_service_labels2) +
tm_text("ID", size = 0.4, col = "gray") +
tm_shape(ng_service_labels3) +
tm_text("ID", size = 0.6, col = "gray", xmod = 2.7, ymod = 0.7) +
# tm_shape(EJlayer) + tm_borders(col = "blue", alpha = 0.7) +
tm_shape(ma_towns_sf_pts) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = 0.7, ymod = 0.2, shadow = TRUE) +
tm_shape(newton) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = -0.7, ymod = 0.2, shadow = TRUE) +
tm_scale_bar(breaks = c(0,25,50), position = c("center","BOTTOM")) +
tm_layout(legend.position = c("left","bottom"),
title = "Massachusetts Gas Leaks per Occupied Housing Unit by Census Block Group and Utility Territories, 2020",
inner.margins = c(0.02,0.02,0.09,0.02)) +
tm_credits("BG = Berkshire Gas\nBGC = Blackstone Gas Co.\nEG = Eversource Gas Co. of MA\nEV = Eversource Energy\nLU = Liberty Utilities\nNG = National Grid\nUN = Unitil/Fitchburg Gas", position = c(.3,.08), col = "gray47", size = 0.7)
tmap_save(m1b, filename = "Images/pub/m_blkgrpOHU2020.png",
dpi = 600, height = 4, width = 8, units = "in")
knitr::include_graphics("Images/pub/m_blkgrpOHU2020.png")
# create map of leaks per OHU by Tract
tractsLeaksOHU <- ma_tracts %>%
filter(ALLleaks_hu > 0)
m2b <- tm_shape(ng_nogas_muni, bbox = ma_state_sf) +
tm_fill(col = "GAS_LABEL", palette = c("honeydew2","gray88"), title = "") +
tm_shape(tractsLeaksOHU) + tm_fill(col = "ALLleaks_hu", style = "fisher",
palette = "YlOrRd",
title = "Leaks per Occupied\nHousing Unit",
legend.format = list(digits = 2)) +
tm_shape(ma_tracts) + tm_borders(lwd = 0.01, alpha = 0.8) +
tm_shape(ng_service_areas2) + tm_borders(lwd = 0.8, col = "grey68", alpha = 0.7) +
tm_shape(ng_service_labels) +
tm_text("ID", size = 0.6, col = "gray") +
tm_shape(ng_service_labels2) +
tm_text("ID", size = 0.4, col = "gray") +
tm_shape(ng_service_labels3) +
tm_text("ID", size = 0.6, col = "gray", xmod = 2.7, ymod = 0.7) +
# tm_shape(EJlayer) + tm_borders(col = "blue", alpha = 0.7) +
tm_shape(ma_towns_sf_pts) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = 0.7, ymod = 0.2, shadow = TRUE) +
tm_shape(newton) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = -0.7, ymod = 0.2, shadow = TRUE) +
tm_scale_bar(breaks = c(0,25,50), position = c("center","BOTTOM")) +
tm_layout(legend.position = c("left","bottom"),
title = "Massachusetts Gas Leaks per Occupied Housing Unit by Census Tract and Utility Territories, 2020",
inner.margins = c(0.02,0.02,0.09,0.02)) +
tm_credits("BG = Berkshire Gas\nBGC = Blackstone Gas Co.\nEG = Eversource Gas Co. of MA\nEV = Eversource Energy\nLU = Liberty Utilities\nNG = National Grid\nUN = Unitil/Fitchburg Gas", position = c(.3,.08), col = "gray47", size = 0.7)
tmap_save(m2b, filename = "Images/pub/m_tractOHU2020.png",
dpi = 600, height = 4, width = 8, units = "in")
knitr::include_graphics("Images/pub/m_tractOHU2020.png")
# create map of leaks per OHU by Cosub
cosubsLeaksOHU <- ma_cosub %>%
filter(ALLleaks_hu > 0)
m2b <- tm_shape(ng_nogas_muni, bbox = ma_state_sf) +
tm_fill(col = "GAS_LABEL", palette = c("honeydew2","gray88"), title = "") +
tm_shape(cosubsLeaksOHU) + tm_fill(col = "ALLleaks_hu", style = "fisher",
palette = "YlOrRd",
title = "Leaks per Occupied\nHousing Unit",
legend.format = list(digits = 2)) +
tm_shape(ma_cosub) + tm_borders(lwd = 0.01, alpha = 0.8) +
tm_shape(ng_service_areas2) + tm_borders(lwd = 0.8, col = "grey68", alpha = 0.7) +
tm_shape(ng_service_labels) +
tm_text("ID", size = 0.6, col = "gray") +
tm_shape(ng_service_labels2) +
tm_text("ID", size = 0.4, col = "gray") +
tm_shape(ng_service_labels3) +
tm_text("ID", size = 0.6, col = "gray", xmod = 2.7, ymod = 0.7) +
# tm_shape(EJlayer) + tm_borders(col = "blue", alpha = 0.7) +
tm_shape(ma_towns_sf_pts) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = 0.7, ymod = 0.2, shadow = TRUE) +
tm_shape(newton) + tm_dots() +
tm_text("NAME", size = 0.4, col = "black",
xmod = -0.7, ymod = 0.2, shadow = TRUE) +
tm_scale_bar(breaks = c(0,25,50), position = c("center","BOTTOM")) +
tm_layout(legend.position = c("left","bottom"),
title = "Massachusetts Gas Leaks per Occupied Housing Unit by Municipality and Utility Territories, 2020",
inner.margins = c(0.02,0.02,0.09,0.02)) +
tm_credits("BG = Berkshire Gas\nBGC = Blackstone Gas Co.\nEG = Eversource Gas Co. of MA\nEV = Eversource Energy\nLU = Liberty Utilities\nNG = National Grid\nUN = Unitil/Fitchburg Gas", position = c(.3,.08), col = "gray47", size = 0.7)
tmap_save(m2b, filename = "Images/pub/m_cosubOHU2020.png",
dpi = 600, height = 4, width = 8, units = "in")
knitr::include_graphics("Images/pub/m_cosubOHU2020.png")
# dot graph of relative exposure for leaks per OHU around zero line
cols <- c("#F7A35C", "#7CB5EC", "gray30")
shps <- c(0,2,1)
# Set up for dot graph to show all three scales
ppLeakDensity_df_bg2 <- ppLeakDensity_df_bg %>%
mutate(Group = factor(Group, levels = group_orderBG)) %>%
pivot_longer(wLeaksPerHURR:wLeaksPerHURRC3,
names_to = "leakClass", values_to = "leakDensityBG") %>%
# filter(!Group %in% c("MA_ENGLISH","MA_MINORITY21","MA_INCOME21")) %>%
mutate(Group = recode(Group, "MA_ENGLISH" = "MA Limited English HH",
"MA_MINORITY21" = "MA Minority",
"MA_INCOME21" = "MA Low Income")) %>%
mutate(leakClass = recode(leakClass, "wLeaksPerHURR" = "All Leaks",
"wLeaksPerHURRC1" = "Class 1 Leaks (high hazard)",
"wLeaksPerHURRC2" = "Class 2 Leaks (med hazard)",
"wLeaksPerHURRC3" = "Class 3 Leaks (low hazard)"),
# Group = reorder_within(Group, leakDensityBG, leakClass),
Status = case_when(
leakDensityBG == 1 ~ "Pop Mean",
leakDensityBG > 1 ~ "Greater Exposure",
leakDensityBG < 1 ~ "Lower Exposure"
),
Scale = "Block Group") %>%
select(Group, leakDensityBG, leakClass, Status, Scale)
# Extract rows with EJ groups to rbind with other dfs
EJrowsTR <- ppLeakDensity_df_bg2 %>%
filter(str_detect(Group, "MA ")) %>%
mutate(leakDensityBG = NA,
Scale = "Tract") %>%
rename(leakDensityTR = leakDensityBG)
EJrowsCO <- ppLeakDensity_df_bg2 %>%
filter(str_detect(Group, "MA ")) %>%
mutate(leakDensityBG = NA,
Scale = "Municipality") %>%
rename(leakDensityCO = leakDensityBG)
# repeat for tracts
ppLeakDensity_df_tr2 <- ppLeakDensity_df_tr %>%
mutate(Group = factor(Group, levels = group_orderTR)) %>%