-
Notifications
You must be signed in to change notification settings - Fork 0
/
ct-covid.R
1132 lines (979 loc) · 44.9 KB
/
ct-covid.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
library(httr)
library(XML)
library(here)
library(fs)
library(RCurl)
library(readr)
library(sf)
library(RSocrata)
library(tabulizer)
library(stringr)
library(lubridate)
library(wordstonumbers)
library(forcats)
library(dplyr)
library(ggplot2)
library(ggpmisc)
library(ggrepel)
library(cowplot)
library(kableExtra)
library(plotly)
library(htmlwidgets)
source(here::here("locals.R"))
#######################
## load up map files ##
#######################
##### GIS data for CT is available here:
## http://magic.lib.uconn.edu/connecticut_data.html
##
## Must first download the relevant shape files by hand, unzip them, and then load them up.
## URL for town shape files is here: http://magic.lib.uconn.edu/magic_2/vector/37800/townct_37800_0000_2010_s100_census_1_shp.zip
ct.shp <-
sf::st_read(here::here("02-shapefiles/CT/townct_37800_0000_2010_s100_census_1_shp/townct_37800_0000_2010_s100_census_1_shp/nad83",
"townct_37800_0000_2010_s100_census_1_shp_nad83_feet.shp")) %>%
filter(NAME10 != "County subdivisions not defined") %>%
mutate(LAT = as.numeric(INTPTLAT10),
LON = as.numeric(INTPTLON10))
#####################################
## download CTDPH daily reports ##
#####################################
## Manually download DPH daily reports at: https://portal.ct.gov/Coronavirus
## TODO: look into writing routine to automate downloads.
## example url for daily COVID-19 update from CT DPH looks like this
## https://portal.ct.gov/-/media/Coronavirus/CTDPHCOVID19summary3282020.pdf?la=en
## Maybe use httr::modify_url to pass in different file names, as needed.
## httr::parse_url("https://portal.ct.gov/-/media/Coronavirus/CTDPHCOVID19summary3282020.pdf?la=en")
#######################################
## Extract info from CTDPH pdf files ##
#######################################
## get list of ctdph covid reports at hand
covid.fnames <- fs::dir_ls(here::here("01-ctdph-daily-reports")) %>%
str_subset("CTDPHCOVID19summary[0-9]+.pdf")
##### read available covid reports and scrape data from them
##### this is necessary to get numbers for early part of the pandemic
scrape.reports = FALSE
town.tab <- TRUE
if(scrape.reports) {
covid <- purrr::map_dfr(covid.fnames, read_ctcovid_pdf, town.tab=town.tab)
saveRDS(covid, file=here::here("03-other-source-data", "pdf-reports.rds"))
} else {
covid <- readRDS(file=here::here("03-other-source-data", "pdf-reports.rds"))
}
##################################################
## Use the Socrata API to access state DPH data ##
##################################################
## David Lucey points out that the data seem to be available more directly on the state's data
## portal at: https://data.ct.gov/stories/s/COVID-19-data/wa3g-tfvc/#data-library
## State data is accessed using the Socrata API. The R package
## RSocrata:: package facilitates this.
##
## initial set up involves
## 1. registering with https://opendata.socrata.com/login
## 2. create an app_token to access the api via read.socrata()
socrata.app.token <- Sys.getenv("SOCRATA_APP_TOKEN_CTCOVID19")
if(FALSE) {
url <- httr::parse_url("https://data.ct.gov/resource/28fr-iqnx.json")
url$path <- NULL
url <- httr::build_url(url)
D.ct <- RSocrata::ls.socrata(url)
D.covid <- D.ct %>%
filter(str_detect(.$title, "COVID"))
}
##### cases and deaths by town
## Upon reviewing the data provided by CT on Nov. 1 2020, for the first time since mid-May, there
## have been some changes to the variables in the Town data file. Need to sort that out before
## pushing new data to the web. Metadata for the Town dataset can be found here
## https://data.ct.gov/Health-and-Human-Services/COVID-19-Tests-Cases-and-Deaths-By-Town-/28fr-iqnx
## OR https://data.ct.gov/resource/28fr-iqnx
covid.api <- read.socrata("https://data.ct.gov/resource/28fr-iqnx.json",
app_token=socrata.app.token) %>%
rename(Town = town,
town.cases = towntotalcases) %>%
mutate(across(starts_with("town", ignore.case=FALSE), as.integer),
across(starts_with("people", ignore.case=FALSE), as.integer),
across(starts_with("number", ignore.case=FALSE), as.integer),
Date = as.Date(lastupdatedate))
if(FALSE) {
covid.api %>%
ggplot(aes(x=Date, color=Town)) +
## geom_line(aes(y=peopletested), legend=FALSE) +
geom_line(aes(y=numberoftests), legend=FALSE)
}
##### scrape town/county data from wikipedia
########### FIXME: STASH THIS TABLE LOCALLY AND USE THAT UNLESS WP PAGE IS UPDATED #########
url <- "https://en.wikipedia.org/wiki/List_of_towns_in_Connecticut"
town.info <- GET(url) %>%
htmlParse() %>%
readHTMLTable(header=TRUE, which=2, skip=170) %>%
janitor::clean_names() %>%
select(-c(form_ofgovernment, native_americanname)) %>%
rename(year.est = dateestablished,
land.area.sq.miles = land_area_square_miles,
pop.2010 = population_in_2010_1,
pop.2020 = population_in_2020_1,
council.of.governments = council_of_governments) %>%
mutate(pop.2010 = as.integer(str_remove(pop.2010, ",")),
land.area.sq.miles = as.numeric(land.area.sq.miles),
county = str_replace(county, "County", "Co."),
pop.2010.bin = cut(pop.2010,
breaks=c(0, 5000, 15000, 35000, 75000, Inf),
labels=c("less than 5,000", "5k, <15k", "15k, <35k", "35k, <75k", "75,000 or more"),
ordered_result=TRUE))
## Merge shapes covid data
ct.covid <-
covid.api %>%
left_join(town.info, by=c("Town" = "name")) %>%
mutate(town.cases.10k = (10000/pop.2010)*town.cases,
town.deaths.10k = (10000/pop.2010)*towntotaldeaths) %>%
left_join(ct.shp, by=c("Town" = "NAME10"))
##### state wide counts
## tests.complete info does not seem to be anywhere in any of the covid-19 data sets provided by the
## state up until late May. The only way to get it is to extract it from their daily reports (pdf
## files).
ct.summary.wide <- read.socrata("https://data.ct.gov/resource/rf3k-f8fg.json",
app_token=socrata.app.token) %>%
rename(Date = date,
Cases.0 = totalcases,
Hospitalized.0 = hospitalizedcases,
Deaths.0 = totaldeaths,
`Tests.0` = covid_19_tests_reported) %>%
mutate(Date = as.Date(Date),
`Tests.0` = as.integer(`Tests.0`),
Cases.0 = as.integer(Cases.0),
Deaths.0 = as.integer(Deaths.0),
Hospitalized.0 = as.integer(Hospitalized.0),
confirmedcasescum = as.integer(confirmedcases),
probablecasescum = as.integer(probablecases),
confirmeddeathscum = as.integer(confirmeddeaths),
probabledeathscum = as.integer(probabledeaths)) %>%
select(-c(state, confirmeddeaths, probabledeaths, confirmedcases, probablecases), -starts_with("cases_"))
## FIXME: this line should be redundant.
## The pdfs have already been scanned at line 66, although with town.tab=TRUE.
## tmp <- purrr::map_dfr(covid.fnames, read_ctcovid_pdf, town.tab=FALSE) %>%
## select(Date, tests.complete)
tmp <- covid %>%
select(Date, tests.complete) %>%
distinct()
ct.summary.wide <- left_join(ct.summary.wide, tmp) %>%
arrange(Date) %>%
mutate(`Tests.0` = if_else(is.na(`Tests.0`), tests.complete, `Tests.0`)) %>%
select(-tests.complete) %>%
mutate(Cases = c(NA, diff(Cases.0)),
Deaths = c(NA, diff(Deaths.0)),
`Tests Reported` = c(NA, diff(`Tests.0`)),
Hospitalized = Hospitalized.0,
`Test Positivity (percent)` = if_else(Date < as.Date("2020-07-01"),
as.numeric(NA),
Cases/`Tests Reported`*100)
) ## Handful of x<0 after this. Let it be.
##### Get what limited school data the state makes available
ct.schools <- read.socrata("https://data.ct.gov/resource/vvjf-9vkr.json",
app_token=socrata.app.token) %>%
mutate_at(vars(ends_with("date")), as.Date) %>%
mutate_at(vars(starts_with("number")), as.integer) %>%
select(-starts_with("difference"), -starts_with("percent")) %>%
tidyr::pivot_longer(cols=starts_with("number"),
names_to = "Group",
values_to = "Count") %>%
mutate(Group = as_factor(str_remove_all(Group, "^number_of_")))
#########################
## constants for plots ##
#########################
caption.ctdph <- paste("Data Source: https://data.ct.gov/stories/s/COVID-19-data/wa3g-tfvc/#data-library.",
"Figure by David Braze ([email protected]) using R statistical software,",
"Released under the Creative Commons v4.0 CC-by license.",
sep="\n")
## file names/types
today <- strftime(today(), "%Y%m%d-")
ftype <- "png"
fig.path <- here::here("figures")
## layout
font.size <- 10
width <- 7
height <- 7
units <- "in"
dpi <- 300
##########################
## school related cases ##
##########################
ct.students.cap <- paste("Weekly Covid19 case count in Connecticut schools for the 2020-21",
"school year, broken down by learning model (in person; remote; hybrid).",
"DPH provides no baselines for these numbers. So, it is unknown",
"how many students are in each learning model, and unknown what",
"proportion of students in each group were actually tested.")
ct.students.plt <-
ct.schools %>%
filter(Group != "student_cases",
Group != "staff_cases") %>%
mutate(Group = fct_relabel(Group, function(x) str_remove(x, "_student_cases|_learning_model_student_cases")),
Group = fct_relevel(Group, rev)) %>%
ggplot(aes(y=Count, x=report_period_start_date)) +
geom_col(aes(fill=Group)) +
scale_x_date(date_labels="%b %d",
date_breaks="1 month",
name="Weekly") +
scale_fill_brewer(type="qual",
palette="Dark2",
name = "Student Group") +
ylab("Number of Cases") +
theme_fdbplot(font_size=font.size) +
background_grid(major="xy") +
theme(plot.margin = unit(c(1,1,1,1), "lines"),
axis.text.x = element_text(angle=45, hjust=1)) +
labs(title="Weekly Covid19 Cases in Ct Public School Students (2020-21 school year)",
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.schools$report_period_end_date), "%B %d, %Y")),
caption=caption.ctdph)
ggsave(filename=fs::path_ext_set(paste0(today, "ct-students"), ftype),
plot=ct.students.plt,
path=fig.path,
device=ftype,
width=width, height=height,
units=units,
dpi=dpi)
################################################
## map 10 day average Test Positivity by Town ##
################################################
## configure the data
ct.covid.positivity.0 <-
ct.covid %>%
group_by(Town) %>%
slice_max(Date, n=11) %>% ## use 11 days instead of 10 so as to catch test count on 1st of 10 days of interest
mutate(ending.date = max(Date),
positive.sum = diff(range(numberofpositives)),
tests.sum = diff(range(numberoftests)),
tests.10k = tests.sum*(10000/pop.2010),
town.positivity = positive.sum/tests.sum*100) %>%
filter(Date == ending.date)
if(FALSE) {
## towns ranked by positivity rate
ct.covid.positivity.0 %>%
ggplot(aes(x=fct_reorder(Town, town.positivity), y=town.positivity)) +
geom_point(aes(size=tests.10k), alpha=1/3) +
theme_fdbplot(font_size=font.size*.7) +
background_grid(major="xy") +
theme(legend.position="top",
plot.margin = unit(c(1,1,1,1), "lines"),
axis.text.x = element_text(angle=45, hjust=1))
}
## ggplot::geom_sf
breaks.0 <- c(0,2,4,6,8,10,12,14,16,18,20)
shade.0 <- max(ct.covid.positivity.0$town.positivity)*.5
map.positivity.cap <- paste("Ten Day Average Covid-19 Test Positivity for each Connecticut Town.",
"Test Positivity is the percentage of tests administered in a town",
"that had a positive result.")
map.positivity <-
ct.covid.positivity.0 %>%
select(town.positivity, geometry, Town, pop.2010, tests.10k) %>%
ggplot() +
geom_sf(aes(fill=town.positivity,
geometry=geometry),
color="white", size=.33) +
geom_sf_text(aes(label=formatC(town.positivity, format="f", digits=2),
geometry=geometry,
color=town.positivity<shade.0,
## 'text' is used for the plotly tooltip
text=paste0(Town,
"\nTest Pos: ", formatC(town.positivity, format="f", digits=2), "%",
"\nPopulation: ", formatC(pop.2010, format="d"),
"\nTests/10k/day: ", formatC(tests.10k/10, format="f", digits=2))),
size=2, show.legend=FALSE) +
scale_color_manual(values=c("black", "white")) +
viridis::scale_fill_viridis(option="magma",
breaks=breaks.0,
labels=breaks.0) +
guides(fill=guide_colorbar(barwidth=10,
title="Test Positivity (%)",
title.vjust=1)) +
labs(title=paste("10 Day Average Covid-19 Test Positivity in Connecticut Towns\nfor period ending",
format(max(ct.covid$Date), "%b %d, %Y")),
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.covid$Date), "%B %d, %Y")),
caption=caption.ctdph) +
xlab(NULL) + ylab(NULL) +
theme_fdbplot(font_size=font.size) +
theme(legend.position="top",
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
ggsave(filename=fs::path_ext_set(paste0(today, "map-positivity"), ftype),
plot=map.positivity,
path=fig.path,
device=ftype,
width=width, height=height,
units=units,
dpi=dpi)
## Make an interactive version ggplot::geom_sf >> plotly::ggplotly
## This is pretty marginal. Here are a couple of tutorials that may
## help make better maps, although they're a bit dated:
## • https://blog.cpsievert.me/2018/03/30/visualizing-geo-spatial-data-with-sf-and-plotly/
## • https://plotly-r.com/maps.html
##
map.positivity.plotly <-
ggplotly(map.positivity,
layerData=2, ## default = 1
tooltip=c("text")) %>%
config(showTips=FALSE,
displayModeBar=FALSE ## hide plotly menubar
## scrollZoom=FALSE ## disable zooming? No.
) %>%
hide_legend() %>%
hide_colorbar()
fqfname <- fs::path(fig.path, fs::path_ext_set(paste0(today, "map-positivity-plotly"), "html"))
saveWidget(map.positivity.plotly, file=fqfname)
if(FALSE) {
## with leaflet
library(leaflet)
leaflet(ct.covid.positivity.0) %>%
addTiles() %>%
setView(-72.8, 41.5, 9) %>%
addPolygons(data=ct.shp)
glimpse(ct.covid.positivity.0)
}
#################################################################
## map cumulative confirmed case count by Town most recent day ##
#################################################################
ct.covid.cumcases <-
ct.covid %>%
filter(Date==max(Date))
breaks.1 <- c(1, 3, 6, 12, 25, 50, 100, 200, 400, 800, 1600, 3200, 6400, 12800)
shade.1 <- exp(log(max(ct.covid.cumcases$town.cases))*.75)
map.cumcases <-
ggplot(ct.covid.cumcases) +
geom_sf(aes(fill=town.cases, geometry=geometry), color="white", size=.33) +
geom_sf_text(aes(label=town.cases,
geometry=geometry,
color=town.cases<shade.1),
size=2, show.legend=FALSE) +
scale_color_manual(values=c("black", "white")) +
scale_fill_continuous(type="viridis",
trans="log",
breaks=breaks.1,
labels=breaks.1) +
guides(fill=guide_colorbar(barwidth=20,
title="Number of Cases",
title.vjust=1)) +
## facet_wrap(~Date,
## ncol=4,
## labeller=label_date) +
labs(title="Cumulative Covid-19 Cases for Connecticut's 169 Towns",
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.covid$Date), "%B %d, %Y")),
caption=caption.ctdph) +
xlab(NULL) + ylab(NULL) +
theme_fdbplot(font_size=font.size) +
theme(legend.position="top",
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks.y = element_blank())
ggsave(filename=fs::path_ext_set(paste0(today, "map-cumcases"), ftype),
plot=map.cumcases,
path=fig.path,
device=ftype,
width=width, height=height,
units=units,
dpi=dpi)
####################################
## rate plot by Town, raw counts ##
####################################
label.cut.towns <-
ct.covid %>%
filter(Date == max(Date)) %>%
select(Town, town.cases, Date) %>%
arrange(desc(town.cases)) %>%
pull(town.cases)
label.count.towns <- 17
highlight <- ct.covid %>%
filter(Town %in% c(""))
town.rate.plt <-
ct.covid %>%
ggplot() +
geom_line(aes(x=Date, y=town.cases, group=Town),
size=1, alpha=1/2, color="blue") +
geom_line(data=highlight, aes(x=Date, y=town.cases, group=Town),
size=1/2, color="darkorange", alpha=1) +
ggrepel::geom_text_repel(data = subset(ct.covid,
Date == max(Date) & town.cases >= label.cut.towns[label.count.towns]),
aes(label = Town, x = Date, y = town.cases),
segment.size=.25,
size=2,
hjust = -0,
direction="y",
force=1/4,
nudge_x=15) +
scale_color_brewer(type="qual", palette="Dark2") +
scale_x_date(date_labels="%b %d",
date_breaks = "1 month",
expand = expansion(mult=c(.01,.15)),
name=NULL) +
labs(title="Cumulative Covid-19 Cases for Connecticut's 169 Towns",
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.covid$Date), "%B %d, %Y")),
caption=caption.ctdph) +
ylab("Number of Cases") +
theme_fdbplot(font_size=font.size) +
background_grid(major="xy") +
theme(legend.position="top",
plot.margin = unit(c(1,1,1,1), "lines"),
axis.text.x = element_text(angle=45, hjust=1))
town.rate.cap <- paste0("Total Covid-19 Cases for Connecticut's 169 Towns.",
"The top ", label.count.towns, " towns are labeled\n",
"(those with at least ", formatC(label.cut.towns[label.count.towns], format="d", big.mark=","), " cases).")
ggsave(filename=fs::path_ext_set(paste0(today, "ct-town-rate"), ftype),
plot=town.rate.plt,
path=fig.path,
device=ftype,
width=width*16/9, height=height,
units=units,
dpi=dpi)
####################################
## rate plot by Town, per 10k pop ##
####################################
label.cut.towns <-
ct.covid %>%
filter(Date == max(Date)) %>%
select(Town, town.cases.10k, Date) %>%
arrange(desc(town.cases.10k)) %>%
pull(town.cases.10k)
label.count.towns <- 17
highlight <- ct.covid %>%
filter(Town %in% c(""))
town.rate.10k.plt <-
ct.covid %>%
ggplot() +
geom_line(aes(x=Date, y=town.cases.10k, group=Town),
size=1, alpha=1/2, color="blue") +
geom_line(data=highlight, aes(x=Date, y=town.cases, group=Town),
size=1/2, color="darkorange", alpha=1) +
ggrepel::geom_text_repel(data = subset(ct.covid,
Date == max(Date) & town.cases.10k >= label.cut.towns[label.count.towns]),
aes(label = Town, x = Date, y = town.cases.10k),
segment.size=.25,
size=2,
hjust = -0,
direction="y",
force=1/4,
nudge_x=15) +
scale_color_brewer(type="qual", palette="Dark2") +
scale_x_date(date_labels="%b %d",
date_breaks="1 month",
expand = expansion(mult=c(.01,.15)),
name=NULL) +
labs(title="Cumulative Covid-19 Cases for Connecticut Towns (per 10k pop.)",
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.covid$Date), "%B %d, %Y")),
caption=caption.ctdph) +
ylab("Number of Cases per 10,000 population") +
theme_fdbplot(font_size=font.size) +
background_grid(major="xy") +
theme(legend.position="top",
plot.margin = unit(c(1,1,1,1), "lines"),
axis.text.x = element_text(angle=45, hjust=1))
town.rate.10k.note <- paste("Cases per 10,000 population for each town.",
"The top ", label.count.towns, " towns are labeled",
"(those with at least", formatC(trunc(label.cut.towns[label.count.towns]), digits=2, format="f", big.mark=","),
"cases per 10k pop.).")
ggsave(filename=fs::path_ext_set(paste0(today, "ct-town-rate-10k"), ftype),
plot=town.rate.10k.plt,
path=fig.path,
device=ftype,
width=width*16/9, height=height,
units=units,
dpi=dpi)
########################################
## rate plot by town, facet by county ##
########################################
label.count <- 6
label.cut <-
ct.covid %>%
filter(Date == max(Date)) %>%
select(Town, county, town.cases, Date) %>%
group_by(county) %>%
arrange(town.cases, .by_group=TRUE) %>%
top_n(label.count, town.cases)
town.by.county.rate.plt <-
ct.covid %>%
ggplot() +
geom_line(aes(x=Date, y=town.cases, group=Town, color=county),
size=1, alpha=1/2) +
facet_wrap(~county, nrow=4, scales="free_y") +
ggrepel::geom_text_repel(data = label.cut,
aes(label = Town, x = Date, y = town.cases),
segment.size=.25,
min.segment.length = 0,
size=2,
hjust = -0,
direction="y",
force=1/4,
nudge_x=15) +
scale_color_brewer(type="qual", palette="Dark2", guide="none") +
scale_y_continuous(limits=my_limits) +
scale_x_date(date_labels="%b %d",
date_breaks="3 months",
expand = expansion(mult=c(.01,.25)),
name=NULL) +
labs(title="Cumulative Covid-19 Cases for Connecticut's 169 Towns, split by county",
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.covid$Date), "%B %d, %Y")),
caption=caption.ctdph) +
ylab("Number of Cases") +
theme_fdbplot(font_size=font.size) +
background_grid(major="xy") +
theme(legend.position="top",
plot.margin = unit(c(1,1,1,1), "lines"),
axis.text.x = element_text(angle=45, hjust=1))
ct.town.by.county.rate.cap <- paste(
"Cumulative Covid-19 case counts by town, split by county.",
"The top ", label.count, " towns in each county are labeled.",
"Note differing y scales for each county.")
ggsave(filename=fs::path_ext_set(paste0(today, "ct-town-by-county-rate"), ftype),
plot=town.by.county.rate.plt,
path=fig.path,
device=ftype,
width=width*16/9, height=height,
units=units,
dpi=dpi)
#############################################################
## rate plot by town per 10k pop., facet by population bin ##
#############################################################
label.count <- 7
label.cut <-
ct.covid %>%
filter(Date == max(Date)) %>%
select(Town, county, pop.2010.bin, town.cases.10k, Date) %>%
group_by(pop.2010.bin) %>%
arrange(town.cases.10k, .by_group=TRUE) %>%
top_n(label.count, town.cases.10k)
town.by.pop.rate10k.plt <-
ct.covid %>%
ggplot() +
geom_line(aes(x=Date, y=town.cases.10k, group=Town, color=county),
size=1, alpha=1/2) +
facet_wrap(~pop.2010.bin, nrow=5, scales="free_y") +
ggrepel::geom_text_repel(data = label.cut,
aes(label = Town, x = Date, y = town.cases.10k, color=county),
alpha=1,
segment.size=.25,
min.segment.length = 0,
size=2,
hjust = -0,
direction="y",
force=1/4,
nudge_x=15,
show.legend=FALSE) +
scale_color_brewer(type="qual", palette="Dark2") +
scale_y_continuous(limits=my_limits) +
scale_x_date(date_labels="%b %d",
date_breaks="1 month",
expand = expansion(mult=c(.01,.15)),
name=NULL) +
labs(title="Cumulative Covid-19 Cases per 10k population for 169 Connecticut Towns\nsplit by population bin",
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.covid$Date), "%B %d, %Y")),
caption=caption.ctdph) +
ylab("Number of Cases per 10,000 Population") +
theme_fdbplot(font_size=font.size) +
background_grid(major="xy") +
guides(color=guide_legend(title="County", title.hjust=0.5, ncol=1,
override.aes=list(size=3, alpha=1),
shape=16)) +
theme(legend.position="right",
plot.margin = unit(c(1,1,1,1), "lines"),
axis.text.x = element_text(angle=45, hjust=1))
ct.town.by.pop.rate10k.cap <- paste(
"Cumulative Covid-19 case counts per capita for each town, split by population bin.",
"The top ", label.count, " towns in each population group are labeled.",
"Note differing y scales for each group.",
"Counties are color coded as before.")
ggsave(filename=fs::path_ext_set(paste0(today, "ct-town-by-pop-rate10k"), ftype),
plot=town.by.pop.rate10k.plt,
path=fig.path,
device=ftype,
width=width*16/9, height=height,
units=units,
dpi=dpi)
################################################
## rate plot by town, facet by population bin ##
################################################
label.count <- 7
label.cut <-
ct.covid %>%
filter(Date == max(Date)) %>%
select(Town, county, pop.2010.bin, town.cases, Date) %>%
group_by(pop.2010.bin) %>%
arrange(town.cases, .by_group=TRUE) %>%
top_n(label.count, town.cases)
town.by.pop.rate.plt <-
ct.covid %>%
ggplot() +
geom_line(aes(x=Date, y=town.cases, group=Town, color=county),
size=1, alpha=1/2) +
facet_wrap(~pop.2010.bin, nrow=5, scales="free_y") +
ggrepel::geom_text_repel(data = label.cut,
aes(label = Town, x = Date, y = town.cases, color=county),
alpha=1,
segment.size=.25,
min.segment.length = 0,
size=2,
hjust = -0,
direction="y",
force=1/4,
nudge_x=10,
show.legend=FALSE) +
scale_color_brewer(type="qual", palette="Dark2") +
scale_y_continuous(limits=my_limits) +
scale_x_date(date_labels="%b %d",
date_breaks="1 month",
expand = expansion(mult=c(.01, .15)),
name=NULL) +
labs(title="Cumulative Covid-19 Cases for Connecticut's 169 Towns, split by population",
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.covid$Date), "%B %d, %Y")),
caption=caption.ctdph) +
ylab("Number of Cases") +
theme_fdbplot(font_size=font.size) +
background_grid(major="xy") +
guides(color=guide_legend(title="County", title.hjust=0.5, ncol=1,
override.aes=list(size=3, alpha=1),
shape=16)) +
theme(legend.position="right",
plot.margin = unit(c(1,1,1,1), "lines"),
axis.text.x = element_text(angle=45, hjust=1))
ct.town.by.pop.rate.cap <- paste(
"Cumulative Covid-19 case counts by town, split by population bin.",
"The top ", label.count, " towns in each population group are labeled.",
"Note differing y scales for each group.",
"Counties are color coded as in the previous plot.")
ggsave(filename=fs::path_ext_set(paste0(today, "ct-town-by-pop-rate"), ftype),
plot=town.by.pop.rate.plt,
path=fig.path,
device=ftype,
width=width*16/9, height=height,
units=units,
dpi=dpi)
########################################################
## line plot for daily change in core statistics. ##
## Includes 7 day running average ##
########################################################
## FIXME:
## o add "(confirmed + probable)" to Cases & Deaths labels
ct.summary.1 <-
ct.summary.wide %>%
select(- c("confirmeddeathscum",
"probabledeathscum",
"confirmedcasescum",
"probablecasescum")) %>%
mutate(`Tests Reported.7mn` = TTR::runMean(`Tests Reported`,7),
Cases.7mn = TTR::runMean(Cases,7),
Hospitalized.7mn = TTR::runMean(Hospitalized,7),
Deaths.7mn = TTR::runMean(Deaths,7),
`Test Positivity (percent).7mn` = TTR::runMean(`Test Positivity (percent)`,7))
ct.summary.long <-
ct.summary.1 %>%
tidyr::pivot_longer(cols=-c(Date)) %>%
## tidyr::separate() might be useful for the following
mutate(type = if_else(str_detect(name, "7mn"), "7mn", "daily"),
name = forcats::fct_collapse(name,
"Tests Reported" = c("Tests Reported", "Tests Reported.7mn"),
"Cases" = c("Cases", "Cases.7mn"),
"Test Positivity (percent)" = c("Test Positivity (percent)", "Test Positivity (percent).7mn"),
"Hospitalized" = c("Hospitalized", "Hospitalized.7mn"),
"Deaths" = c("Deaths", "Deaths.7mn"))) %>%
mutate(name = forcats::fct_relevel(name, "Tests Reported",
"Cases", "Test Positivity (percent)",
"Hospitalized", "Deaths"),
type = forcats::fct_relevel(type, "daily", "7mn"))
ct.stats.label <-
ct.summary.long %>%
filter(! name %in% c("Tests.0", "Cases.0", "Hospitalized.0", "Deaths.0")) %>%
filter(Date == max(Date)) %>%
mutate(label = formatC(value, digits=2, format="f", big.mark=","),
label = if_else(type == "7mn",
label,
str_replace(label, "\\.00", "")))
ct.stat.daily.change.plt <-
ct.summary.long %>%
filter(! name %in% c("Tests.0", "Cases.0", "Hospitalized.0", "Deaths.0")) %>% ## glimpse()
ggplot(aes(x=Date)) +
geom_line(aes(y=value, color=name, linetype=type, size=type, alpha=type), show.legend=FALSE) +
scale_size_manual(values=c(1,.75)) +
scale_alpha_manual(values=c(1/3,1)) +
scale_linetype_manual(values=c("solid", "F2")) +
scale_x_date(date_labels="%b %d",
date_breaks="1 month",
expand = expansion(mult=c(.01,.1)),
name=NULL) +
scale_y_continuous(limits=my_limits) +
scale_color_manual(values=RColorBrewer::brewer.pal(5,"Dark2")[c(1,2,5,3,4)]) +
facet_wrap(~name, nrow=5, scales="free_y") +
labs(title="Daily Values for Covid-19 Statistics in Connecticut",
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.covid$Date), "%B %d, %Y")),
caption=caption.ctdph) +
ylab("Daily Value (non-cumulative)") + xlab(NULL) +
theme_fdbplot(font_size=font.size) +
background_grid(major="xy") +
theme(axis.text.x = element_text(angle=45, hjust=1)) +
ggrepel::geom_text_repel(data = ct.stats.label,
aes(label = label, x = Date, y = value),
segment.size=.25,
min.segment.length = 0,
size=2.5,
hjust = 0,
direction="y",
force=1/4,
nudge_x=15)
ct.stat.daily.change.cap <- paste(
"Daily Values (non-cumulative) for Covid-19 Statistics.",
"Pale solid lines = raw counts.",
"Dark dashed lines = 7 day running averages.",
"Tests before July 1 were in short supply and largely limited to those suspected of being infected.",
"So, 'Test Positivity' for the period before July 1 is not shown.",
"Note different y scales on subplots.")
ggsave(filename=fs::path_ext_set(paste0(today, "ct-summary-rate"), ftype),
plot=ct.stat.daily.change.plt,
path=fig.path,
device=ftype,
width=width, height=height,
units=units,
dpi=dpi)
if(FALSE){
plotly::ggplotly(ct.stat.daily.change.plt)
tmp <-
ct.summary.wide %>%
filter(Date >= "2020-10-01")
Hmisc::rcorr(tmp$`Tests Reported`, tmp$`Test Positivity (percent)`)
}
#####################################
## Drill down into test positivity ##
#####################################
if (FALSE) {
ct.tpos.label <-
ct.summary.long %>%
filter(name == "Test Positivity (percent)") %>%
filter(Date == max(Date)) %>%
mutate(label = formatC(value, digits=2, format="f", big.mark=","),
label = if_else(type == "7mn",
label,
str_replace(label, "\\.00", "")))
holidays <- tibble(name = c("Independence Day", "Labor Day", "Columbus Day", "Thanksgiving"),
start = as.Date(c("2020-07-03", "2020-09-05", "2020-10-10", "2020-11-26")),
end = as.Date(c("2020-07-05", "2020-09-07", "2020-10-12", "2020-11-29")),
ymin = 0,
ymax = 7)
ct.tpos.daily.change.plt <-
ct.summary.long %>%
filter(name == "Test Positivity (percent)") %>% ## glimpse()
ggplot(aes(x=Date)) +
geom_line(aes(y=value, color=name, linetype=type, size=type, alpha=type), show.legend=FALSE) +
scale_size_manual(values=c(1,.75)) +
scale_alpha_manual(values=c(1/3,1)) +
scale_linetype_manual(values=c("solid", "F2")) +
scale_x_date(date_labels="%b %d",
date_breaks="1 month",
expand = expansion(add=c(2,30)),
name=NULL) +
scale_y_continuous(limits=my_limits) +
scale_color_manual(values=RColorBrewer::brewer.pal(5,"Dark2")[c(1,2,5,3,4)]) +
geom_rect(data=holidays, inherit.aes=FALSE,
aes(xmin=start, xmax=end, ymin=ymin, ymax=ymax),
fill="orange", color=NA, alpha=1/4) +
labs(title="Covid-19 Test Positivity in Connecticut",
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.covid$Date), "%B %d, %Y")),
caption=caption.ctdph) +
ylab("Test Positivity Value (percent)") + xlab(NULL) +
theme_fdbplot(font_size=font.size) +
background_grid(major="xy") +
theme(axis.text.x = element_text(angle=45, hjust=1)) +
ggrepel::geom_text_repel(data = ct.tpos.label,
aes(label = label, x = Date, y = value),
segment.size=.25,
min.segment.length = 0,
size=2.5,
hjust = 0,
direction="y",
force=1/4,
nudge_x=10)
ct.tpos.daily.change.cap <- paste(
"Daily Values for Covid-19 Test Positivity.",
"Pale solid line = raw percent.",
"Dark dashed line = 7 day running average.",
"Tests before July 1 were in short supply and largely limited to those suspected of being infected.",
"So, 'Test Positivity' for the period before July 1 is not shown.")
ggsave(filename=fs::path_ext_set(paste0(today, "ct-summary-tpos"), ftype),
plot=ct.tpos.daily.change.plt,
path=fig.path,
device=ftype,
width=width, height=height,
units=units,
dpi=dpi)
}
####################################
## bar plot 2 for state-wide data ##
####################################
ct.stats.label.2 <-
ct.summary.long %>%
filter(name %in% c("Tests.0", "Cases.0", "Hospitalized.0", "Deaths.0")) %>%
filter(Date == max(Date)) %>%
mutate(name = fct_recode(name,
"Cases, cumulative" = "Cases.0",
"Tests, cumulative" = "Tests.0",
"Hospitalized, daily" = "Hospitalized.0",
"Deaths, cumulative" = "Deaths.0")) %>%
mutate(label = formatC(value, format="d", big.mark=","))
ct.summary.2.plt <-
ct.summary.long %>%
filter(name %in% c("Cases.0", "Tests.0", "Hospitalized.0", "Deaths.0" )) %>% ## glimpse()
mutate(name = fct_recode(name,
"Cases, cumulative" = "Cases.0",
"Tests, cumulative" = "Tests.0",
"Hospitalized, daily" = "Hospitalized.0",
"Deaths, cumulative" = "Deaths.0")) %>%
mutate(name = forcats::fct_relevel(name,
"Tests, cumulative",
"Cases, cumulative",
"Hospitalized, daily",
"Deaths, cumulative")) %>%
ggplot(aes(y=value, x=Date)) +
geom_bar(aes(fill=name), alpha=1/2, position="dodge", stat="identity", show.legend=FALSE) +
facet_wrap(~name, nrow=4, scales="free_y") +
scale_fill_brewer(type="qual", palette="Dark2") +
scale_x_date(expand = expansion(add=c(2, 30)),
date_labels="%b %d",
date_breaks="1 month",
name=NULL) +
scale_color_brewer(type="qual", palette="Dark2") +
ylim(-150, NA) +
guides(fill=guide_legend(title=NULL)) +
labs(title="Covid-19 Cases, Hospitalizations, Deaths, & Tests for Connecticut",
subtitle=paste("Data compiled by CT Dept. of Public Health through",
format(max(ct.covid$Date), "%B %d, %Y")),
caption=caption.ctdph) +
ylab("Count") + xlab(NULL) +
theme_fdbplot(font_size=font.size) +
background_grid(major="xy") +
theme(legend.position=c(.05, .90),
axis.text.x = element_text(angle=45, hjust=1)) +
ggrepel::geom_text_repel(data = ct.stats.label.2,
aes(label = label, x = Date, y = value),
segment.size=.25,
min.segment.length = 0,
size=2.5,
hjust = 0,
direction="y",
force=1/4,
nudge_x=10)
ggsave(filename=fs::path_ext_set(paste0(today, "ct-summary"), ftype),
plot=ct.summary.2.plt,
path=fig.path,
device=ftype,