-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcovid.Rmd
700 lines (567 loc) · 24.4 KB
/
covid.Rmd
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
---
title: "COVID"
author:
- name: Kieran Healy
affiliation: Duke University
email: kjhealy@soc.duke.edu
date: '`r format(Sys.Date(), "%B %d, %Y")`'
crossrefYaml: "config/pandoc-crossref-settings.yaml"
output:
pdf_document:
md_extensions: +simple_tables+table_captions+yaml_metadata_block+smart
template: /Users/kjhealy/.pandoc/templates/rmd-latex.template
pandoc_args: [
"--bibliography", "/Users/kjhealy/Documents/bibs/socbib-pandoc.bib",
"--filter", "pandoc-crossref",
"--filter", "pandoc-citeproc",
"--csl", "/Users/kjhealy/.pandoc/csl/ajps.csl"
]
html_document: distill::distill_article
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
fig.showtext = TRUE)
```
```{r libraries, echo = TRUE}
library(tidyverse)
library(lubridate)
library(here)
library(janitor)
library(socviz)
library(ggrepel)
library(paletteer)
## --------------------------------------------------------------------
## Custom font and theme, omit if you don't have the myriad library
## (https://github.com/kjhealy/myriad) and associated Adobe fonts.
## --------------------------------------------------------------------
library(showtext)
showtext_auto()
library(myriad)
import_myriad_semi()
theme_covid <- function() {
theme_myriad_semi() +
theme(plot.title = element_text(size = rel(2), face = "bold"),
plot.subtitle = element_text(size = rel(1.5)),
plot.caption = element_text(size = rel(0.9)),
axis.text.y = element_text(size = rel(2)),
axis.title.x = element_text(size = rel(1.5)),
axis.title.y = element_text(size = rel(1.5)),
axis.text.x = element_text(size = rel(2)),
legend.text = element_text(size = rel(2))
)
}
theme_set(theme_covid())
### --------------------------------------------------------------------
## cb-friendly palette
col_pal <- c("#E69F00", "#0072B2", "#000000", "#56B4E9",
"#009E73", "#F0E442", "#D55E00", "#CC79A7")
```
```{r functions}
## Download today's CSV file, saving it to data/ and also read it in
get_ecdc_csv <- function(url = "https://opendata.ecdc.europa.eu/covid19/casedistribution/csv",
date = lubridate::today(),
writedate = lubridate::today(),
fname = "ecdc-cumulative-",
ext = "csv",
dest = "data") {
target <- url
message("target: ", target)
destination <- fs::path(here::here("data"), paste0(fname, writedate), ext = ext)
message("saving to: ", destination)
tf <- tempfile(fileext = ext)
curl::curl_download(target, tf)
fs::file_copy(tf, destination)
janitor::clean_names(readr::read_csv(tf))
}
## Get Daily COVID Tracking Project Data
## form is https://covidtracking.com/api/us/daily.csv
get_uscovid_data <- function(url = "https://covidtracking.com/api/",
unit = c("states", "us"),
fname = "-",
date = lubridate::today(),
ext = "csv",
dest = "data/us_covid") {
unit <- match.arg(unit)
target <- paste0(url, unit, "/", "daily.", ext)
message("target: ", target)
destination <- fs::path(here::here("data/covid_us"),
paste0(unit, "_daily_", date), ext = ext)
message("saving to: ", destination)
tf <- tempfile(fileext = ext)
curl::curl_download(target, tf)
fs::file_copy(tf, destination)
janitor::clean_names(read_csv(tf))
}
## Which n states are leading the count of positive cases or deaths?
top_n_states <- function(data, n = 5, measure = c("positive", "death")) {
meas <- match.arg(measure)
data %>%
group_by(state) %>%
filter(measure == meas, date == max(date)) %>%
drop_na() %>%
ungroup() %>%
top_n(n, wt = count) %>%
pull(state)
}
## A useful function from Edward Visel, which does a thing
## with tibbles that in the past I've done variable-by-variable
## using match(), like an animal. The hardest part was
## figuring out that this operation is called a coalescing join
## https://alistaire.rbind.io/blog/coalescing-joins/
coalesce_join <- function(x, y,
by = NULL, suffix = c(".x", ".y"),
join = dplyr::full_join, ...) {
joined <- join(x, y, by = by, suffix = suffix, ...)
# names of desired output
cols <- dplyr::union(names(x), names(y))
to_coalesce <- names(joined)[!names(joined) %in% cols]
suffix_used <- suffix[ifelse(endsWith(to_coalesce, suffix[1]), 1, 2)]
# remove suffixes and deduplicate
to_coalesce <- unique(substr(
to_coalesce,
1,
nchar(to_coalesce) - nchar(suffix_used)
))
coalesced <- purrr::map_dfc(to_coalesce, ~dplyr::coalesce(
joined[[paste0(.x, suffix[1])]],
joined[[paste0(.x, suffix[2])]]
))
names(coalesced) <- to_coalesce
dplyr::bind_cols(joined, coalesced)[cols]
}
```
```{r iso-country-codes}
## Country codes. The ECDC does not quite use standard codes for countries
## These are the iso2 and iso3 codes, plus some convenient groupings for
## possible use later
iso3_cnames <- read_csv("data/countries_iso3.csv")
iso2_to_iso3 <- read_csv("data/iso2_to_iso3.csv")
cname_table <- left_join(iso3_cnames, iso2_to_iso3)
cname_table
eu <- c("AUT", "BEL", "BGR", "HRV", "CYP", "CZE", "DNK", "EST", "FIN", "FRA",
"DEU", "GRC", "HUN", "IRL", "ITA", "LVA", "LTU", "LUX", "MLT", "NLD",
"POL", "PRT", "ROU", "SVK", "SVN", "ESP", "SWE", "GBR")
europe <- c("ALB", "AND", "AUT", "BLR", "BEL", "BIH", "BGR", "HRV", "CYP", "CZE",
"DNK", "EST", "FRO", "FIN", "FRA", "DEU", "GIB", "GRC", "HUN", "ISL",
"IRL", "ITA", "LVA", "LIE", "LTU", "LUX", "MKD", "MLT", "MDA", "MCO",
"NLD", "NOR", "POL", "PRT", "ROU", "RUS", "SMR", "SRB", "SVK", "SVN",
"ESP", "SWE", "CHE", "UKR", "GBR", "VAT", "RSB", "IMN", "MNE")
north_america <- c("AIA", "ATG", "ABW", "BHS", "BRB", "BLZ", "BMU", "VGB", "CAN", "CYM",
"CRI", "CUB", "CUW", "DMA", "DOM", "SLV", "GRL", "GRD", "GLP", "GTM",
"HTI", "HND", "JAM", "MTQ", "MEX", "SPM", "MSR", "ANT", "KNA", "NIC",
"PAN", "PRI", "KNA", "LCA", "SPM", "VCT", "TTO", "TCA", "VIR", "USA",
"SXM")
south_america <- c("ARG", "BOL", "BRA", "CHL", "COL", "ECU", "FLK", "GUF", "GUY", "PRY",
"PER", "SUR", "URY", "VEN")
africa <- c("DZA", "AGO", "SHN", "BEN", "BWA", "BFA", "BDI", "CMR", "CPV", "CAF",
"TCD", "COM", "COG", "DJI", "EGY", "GNQ", "ERI", "ETH", "GAB", "GMB",
"GHA", "GNB", "GIN", "CIV", "KEN", "LSO", "LBR", "LBY", "MDG", "MWI",
"MLI", "MRT", "MUS", "MYT", "MAR", "MOZ", "NAM", "NER", "NGA", "STP",
"REU", "RWA", "STP", "SEN", "SYC", "SLE", "SOM", "ZAF", "SHN", "SDN",
"SWZ", "TZA", "TGO", "TUN", "UGA", "COD", "ZMB", "TZA", "ZWE", "SSD",
"COD")
asia <- c("AFG", "ARM", "AZE", "BHR", "BGD", "BTN", "BRN", "KHM", "CHN", "CXR",
"CCK", "IOT", "GEO", "HKG", "IND", "IDN", "IRN", "IRQ", "ISR", "JPN",
"JOR", "KAZ", "PRK", "KOR", "KWT", "KGZ", "LAO", "LBN", "MAC", "MYS",
"MDV", "MNG", "MMR", "NPL", "OMN", "PAK", "PHL", "QAT", "SAU", "SGP",
"LKA", "SYR", "TWN", "TJK", "THA", "TUR", "TKM", "ARE", "UZB", "VNM",
"YEM", "PSE")
oceania <- c("ASM", "AUS", "NZL", "COK", "FJI", "PYF", "GUM", "KIR", "MNP", "MHL",
"FSM", "UMI", "NRU", "NCL", "NZL", "NIU", "NFK", "PLW", "PNG", "MNP",
"SLB", "TKL", "TON", "TUV", "VUT", "UMI", "WLF", "WSM", "TLS")
```
## European Centers for Disease Control Data
```{r get-and-clean-data}
## There's a typo in the file name. Note 'disbtribution' rather than 'distribution'
## e.g. COVID-19-geographic-disbtribution-worldwide-2020-03-18.xls
## I assume this'll get fixed or changed at some point.
## The good thing is that these data files are cumulative
## on 3/25/20 they changed the file name to omit the date
# covid_raw <- get_ecdc_data(url = "https://www.ecdc.europa.eu/sites/default/files/documents/",
# fname = "COVID-19-geographic-disbtribution-worldwide-",
# ext = "xlsx")
## on 3/27/2020 they broadened the data formats and changed the URLS
covid_raw <- get_ecdc_csv()
covid_raw
## on 3/27/20 they changed the contents of the file too, including the date format
covid <- covid_raw %>%
mutate(date = lubridate::dmy(date_rep),
iso2 = geo_id)
covid
## merge in the iso country names
covid <- left_join(covid, cname_table)
covid
## Looks like a missing data code
## Also note that not everything in this dataset is a country
covid %>%
filter(cases == -9)
## A few ECDC country codes are non-iso, notably the UK
anti_join(covid, cname_table) %>%
select(geo_id, countries_and_territories, iso2, iso3, cname) %>%
distinct()
## A small crosswalk file that we'll coalesce into the missing values
## We need to specify the na explicity because the xwalk file has Namibia
## as a country -- i.e. country code = string literal "NA"
cname_xwalk <- read_csv("data/ecdc_to_iso2_xwalk.csv",
na = "")
cname_xwalk
## How to do this manually
# covid <- covid %>%
# left_join(cname_xwalk, by = "geo_id") %>%
# mutate(iso3 = coalesce(iso3.x, iso3.y),
# cname = coalesce(cname.x, cname.y)) %>%
# select(-iso3.x, -iso3.y, cname.x, cname.y)
## But nicer to use the function from Edward Visel
covid <- coalesce_join(covid, cname_xwalk,
by = "geo_id", join = dplyr::left_join)
## Take a look again
anti_join(covid, cname_table) %>%
select(geo_id, countries_and_territories, iso2, iso3, cname) %>%
distinct()
```
```{r plots}
## cumulative cases for selected countries
cu_out <- covid %>%
filter(iso3 %in% c("ITA", "CHN", "GBR", "FRA",
"USA", "KOR")) %>%
group_by(cname) %>%
arrange(date) %>%
mutate(cases_na = na_if(cases, 0),
deaths_na = na_if(deaths,0),
cu_cases = cumsum(cases),
cu_deaths = cumsum(deaths)) %>%
select(date, cname, cu_cases, cu_deaths) %>%
pivot_longer(cu_cases:cu_deaths,
names_to = "measure", values_to = "count") %>%
mutate(measure = recode(measure, `cu_cases` = "Cases",
`cu_deaths` = "Deaths")) %>%
mutate(count = na_if(count, 0))
cu_out %>%
arrange(desc(date))
cu_out %>%
ggplot(mapping = aes(x = date, y = count,
color = measure)) +
geom_line(size = 1.5) +
scale_color_manual(values = col_pal) +
# scale_y_continuous(labels = scales::comma_format(accuracy = 1)) +
scale_y_continuous(trans = "log2",
labels = scales::comma_format(accuracy = 1),
breaks = 2^c(seq(1, 17, 2))) +
# scale_y_continuous(trans = "log10",
# labels = scales::comma_format(accuracy = 1)) +
labs(title = paste0("COVID-19 Cumulative Recorded Cases and Deaths to ",
max(cu_out$date)),
x = "Date", y = "Cumulative Reported Events (log 2 scale)", color = "Measure") +
facet_wrap(~ reorder(cname, -count, na.rm = TRUE)) +
theme(legend.position = "top")
## Just deaths
cu_out %>%
filter(measure == "Deaths") %>%
ggplot(mapping = aes(x = date, y = count)) +
geom_line(size = 1.5) +
# scale_y_continuous(labels = scales::comma_format(accuracy = 1)) +
scale_y_continuous(trans = "log2",
labels = scales::comma_format(accuracy = 1),
breaks = 2^c(seq(1, 17, 2))) +
# scale_y_continuous(trans = "log10",
# labels = scales::comma_format(accuracy = 1)) +
labs(title = paste0("COVID-19 Cumulative Recorded Deaths to ",
max(cu_out$date)),
x = "Date", y = "Cumulative Reported Events (log 2 scale)") +
facet_wrap(~ reorder(cname, -count, na.rm = TRUE)) +
theme(legend.position = "top")
```
```{r, layout="l-body-outset", fig.width=10, fig.height=8}
## The graph everyone draws
cov_curve <- covid %>%
select(date, cname, iso3, cases, deaths) %>%
drop_na(iso3) %>%
group_by(iso3) %>%
arrange(date) %>%
mutate(cu_cases = cumsum(cases),
cu_deaths = cumsum(deaths)) %>%
filter(cu_deaths > 9) %>%
mutate(days_elapsed = date - min(date),
end_label = ifelse(date == max(date), cname, NA),
end_label = recode(end_label, `United States` = "USA",
`Iran, Islamic Republic of` = "Iran",
`Korea, Republic of` = "South Korea",
`United Kingdom` = "UK"),
cname = recode(cname, `United States` = "USA",
`Iran, Islamic Republic of` = "Iran",
`Korea, Republic of` = "South Korea",
`United Kingdom` = "UK"))
cov_curve
focus_cn <- c("CHN", "DEU", "GBR", "USA", "IRN", "JPN",
"KOR", "ITA", "FRA", "ESP", "CHE", "TUR")
## Colors
cgroup_cols <- c(prismatic::clr_darken(paletteer_d("ggsci::category20_d3"), 0.2)[1:length(focus_cn)], "gray70")
(p_death_curve <- cov_curve %>%
mutate(end_label = case_when(iso3 %in% focus_cn ~ end_label,
TRUE ~ NA_character_),
cgroup = case_when(iso3 %in% focus_cn ~ iso3,
TRUE ~ "ZZOTHER")) %>%
ggplot(mapping = aes(x = days_elapsed, y = cu_deaths,
color = cgroup, label = end_label,
group = cname)) +
geom_line(size = 0.5) +
geom_text_repel(nudge_x = 1.1,
nudge_y = 0.1,
segment.color = NA) +
guides(color = FALSE) +
scale_color_manual(values = cgroup_cols) +
scale_y_continuous(labels = scales::comma_format(accuracy = 1),
breaks = 2^seq(4, 14),
trans = "log2") +
labs(x = "Days Since 10th Confirmed Death",
y = "Cumulative Number of Deaths (log2 scale)",
title = "Cumulative Number of Reported Deaths from COVID-19, Selected Countries",
subtitle = paste("Data as of", format(max(cov_curve$date), "%A, %B %e, %Y")),
caption = "Kieran Healy @kjhealy / Data: https://www.ecdc.europa.eu/")
)
```
```{r table}
cov_curve %>%
group_by(iso3) %>%
filter(cu_deaths == max(cu_deaths)) %>%
arrange(desc(cu_deaths)) %>%
select(cname, cu_cases, cu_deaths, days_elapsed)
```
```{r, layout="l-body-outset", fig.width=10, fig.height=8}
cov_case_curve <- covid %>%
select(date, cname, iso3, cases, deaths) %>%
drop_na(iso3) %>%
group_by(iso3) %>%
arrange(date) %>%
mutate(cu_cases = cumsum(cases),
cu_deaths = cumsum(deaths)) %>%
filter(cu_cases > 99) %>%
mutate(days_elapsed = date - min(date),
end_label = ifelse(date == max(date), cname, NA),
end_label = recode(end_label, `United States` = "USA",
`Iran, Islamic Republic of` = "Iran",
`Korea, Republic of` = "South Korea",
`United Kingdom` = "UK"),
cname = recode(cname, `United States` = "USA",
`Iran, Islamic Republic of` = "Iran",
`Korea, Republic of` = "South Korea",
`United Kingdom` = "UK"))
(p_case_curve <- cov_case_curve %>%
mutate(end_label = case_when(iso3 %in% focus_cn ~ end_label,
TRUE ~ NA_character_),
cgroup = case_when(iso3 %in% focus_cn ~ iso3,
TRUE ~ "ZZOTHER")) %>%
ggplot(mapping = aes(x = days_elapsed, y = cu_cases,
color = cgroup, label = end_label,
group = cname)) +
geom_line(size = 0.5) +
geom_text_repel(nudge_x = 0.75,
segment.color = NA) +
guides(color = FALSE) +
scale_color_manual(values = cgroup_cols) +
scale_y_continuous(labels = scales::comma_format(accuracy = 1),
breaks = 2^seq(4, 19, 1),
trans = "log2") +
labs(x = "Days Since 100th Confirmed Case",
y = "Cumulative Number of Reported Cases (log2 scale)",
title = "Cumulative Reported Cases of COVID-19, Selected Countries",
subtitle = paste("ECDC data as of", format(max(cov_curve$date), "%A, %B %e, %Y")),
caption = "Kieran Healy @kjhealy / Data: https://www.ecdc.europa.eu/")
)
ggsave("figures/cov_case_grouped.png", p_case_curve,
width = 10, height = 8, dpi = 300)
```
```{r like-jbm, layout="l-body-outset", fig.width=10, fig.height=12}
## Replicate JB Murdoch's small multiple, only don't alphabetize countries.
## https://www.ft.com/coronavirus-latest
## Top N countries by >> 100 cases, let's say.
n_countries <- 50
country_panels <- cov_case_curve %>%
group_by(cname) %>%
filter(cu_cases == max(cu_cases)) %>%
filter(row_number() == 1) %>%
ungroup() %>%
top_n(n_countries, cu_cases) %>%
select(iso3, cname, cu_cases) %>%
mutate(days_elapsed = 1,
cu_cases = max(cov_case_curve$cu_cases) - 9e4)
country_panels
cov_case_curve_bg <- cov_case_curve %>%
select(-cname) %>%
filter(iso3 %in% country_panels$iso3)
cov_case_curve_endpoints <- cov_case_curve %>%
filter(iso3 %in% country_panels$iso3) %>%
group_by(iso3) %>%
arrange(desc(date)) %>%
filter(cu_cases == max(cu_cases)) %>%
filter(row_number() == 1) %>%
select(cname, iso3, days_elapsed, cu_cases) %>%
ungroup()
(p_cov_case_sm <- cov_case_curve %>%
filter(iso3 %in% country_panels$iso3) %>%
ggplot(mapping = aes(x = days_elapsed, y = cu_cases)) +
geom_line(data = cov_case_curve_bg,
aes(group = iso3),
size = rel(0.2), color = "gray80") +
geom_line(color = "firebrick",
lineend = "round") +
geom_point(data = cov_case_curve_endpoints,
size = rel(1),
shape = 21,
color = "firebrick",
fill = "firebrick2"
) +
geom_text(data = country_panels,
mapping = aes(label = cname),
vjust = "inward",
hjust = "inward",
fontface = "bold",
color = "firebrick",
size = rel(1.85)) +
scale_y_log10(labels = scales::label_number_si()) +
facet_wrap(~ reorder(cname, -cu_cases), ncol = 5) +
labs(x = "Days Since 100th Confirmed Case",
y = "Cumulative Number of Cases (log10 scale)",
title = "Cumulative Number of Reported Cases of COVID-19: Selected Countries",
subtitle = paste("Data as of", format(max(cov_curve$date), "%A, %B %e, %Y")),
caption = "Kieran Healy @kjhealy / Data: https://www.ecdc.europa.eu/") +
theme(plot.title = element_text(size = rel(1), face = "bold"),
plot.subtitle = element_text(size = rel(0.7)),
plot.caption = element_text(size = rel(1)),
strip.text = element_blank(),
panel.spacing.x = unit(-0.05, "lines"),
panel.spacing.y = unit(0.3, "lines"),
axis.text.y = element_text(size = rel(0.5)),
axis.title.x = element_text(size = rel(1)),
axis.title.y = element_text(size = rel(1)),
axis.text.x = element_text(size = rel(0.5)),
legend.text = element_text(size = rel(1)))
)
ggsave("figures/cov_case_sm.pdf",
p_cov_case_sm, width = 8, height = 12)
ggsave("figures/cov_case_sm.png",
p_cov_case_sm, width = 8, height = 12, dpi = 300)
```
## Plotly Example
```{r, plotly, eval = FALSE}
library(plotly)
country_plotly <- cov_case_curve %>%
ungroup() %>%
select(date, cname, cu_cases, cu_deaths, days_elapsed) %>%
rename(Country = cname, Date = date, Cases = cu_cases, Deaths = cu_deaths, Days = days_elapsed)
country_plotly <- highlight_key(country_plotly, ~ Country)
(p_cov_case_sm <-
ggplot(data = country_plotly,
mapping = aes(x = Days, y = Cases, group = Country)) +
geom_line(lineend = "round", size = 0.15) +
scale_y_log10(labels = scales::label_number_si()) +
labs(x = "Days Since 100th Confirmed Case",
y = "Cumulative Number of Cases (log10 scale)") +
theme_minimal()
)
gg <- ggplotly(p_cov_case_sm, tooltip = c("Country", "Days", "Cases"))
plotly_cases <- layout(highlight(gg),
font = "inherit")
(p_cov_deaths_sm <-
ggplot(data = country_plotly,
mapping = aes(x = Days, y = Deaths, group = Country)) +
geom_line(lineend = "round", size = 0.15) +
scale_y_log10(labels = scales::label_number_si()) +
labs(x = "Days Since 10th Confirmed Death",
y = "Cumulative Number of Deaths (log10 scale)") +
theme_minimal()
)
gg <- ggplotly(p_cov_deaths_sm, tooltip = c("Country", "Days", "Deaths"))
plotly_deaths <- layout(highlight(gg), font = "inherit")
library(htmlwidgets)
library(widgetframe)
saveWidget(frameableWidget(partial_bundle(plotly_cases)), "p1.html",
selfcontained = F, libdir = "javascripts")
saveWidget(frameableWidget(partial_bundle(plotly_deaths)), "p2.html",
selfcontained = F, libdir = "javascripts")
```
## US State-Level Data from [https://covidtracking.com]
```{r, layout="l-body-outset", fig.width=5, fig.height=4}
us_states_raw <- get_uscovid_data()
us_states <- us_states_raw %>%
mutate(date = lubridate::ymd(date)) %>%
select(-hash, -date_checked) %>%
pivot_longer(positive:total_test_results,
names_to = "measure", values_to = "count")
max(us_states$date)
us_states %>%
filter(measure == "positive", date == max(date)) %>%
drop_na() %>%
ungroup() %>%
arrange(desc(count))
us_states %>%
group_by(state) %>%
filter(measure == "positive", date == max(date)) %>%
drop_na() %>%
ungroup() %>%
arrange(desc(count)) %>%
top_n(10, count) %>%
ggplot(aes(x = count, y = reorder(state, count))) +
geom_point(size = 3) +
scale_x_continuous(trans = "log2",
breaks = 2^c(seq(1, 17, 1)),
labels = scales::comma_format(accuracy = 1)) +
labs(title = "Total Recorded Cases to Date",
subtitle = paste("Top 10 States. Data as of", format(max(us_states$date), "%A, %B %e, %Y")),
caption = "Data: COVID Tracking Project, http://covidtracking.com | Graph: @kjhealy",
y = NULL,
x = "Count (log2 scale)")
```
```{r, layout="l-body-outset", fig.width=10, fig.height=8}
state_cols <- c("gray70",
prismatic::clr_darken(paletteer_d("ggsci::category20_d3"), 0.2))
(p_state_cases <- us_states %>%
group_by(state) %>%
mutate(core = case_when(state %nin% top_n_states(us_states) ~ "",
TRUE ~ state),
end_label = ifelse(date == max(date), core, NA)) %>%
arrange(date) %>%
filter(measure == "positive", date > "2020-03-09") %>%
ggplot(aes(x = date, y = count, group = state, color = core, label = end_label)) +
geom_line(size = 0.5) +
geom_text_repel(segment.color = NA, nudge_x = 0.2, nudge_y = 0.1) +
scale_color_manual(values = state_cols) +
scale_x_date(date_breaks = "3 days", date_labels = "%b %e" ) +
scale_y_continuous(trans = "log2",
labels = scales::comma_format(accuracy = 1),
breaks = 2^c(seq(1, 17, 1))) +
guides(color = FALSE) +
coord_equal() +
labs(title = "COVID-19 Cumulative Recorded Cases by US State",
subtitle = paste("Data as of", format(max(us_states$date), "%A, %B %e, %Y")),
x = "Date", y = "Count of Cases (log 2 scale)",
caption = "Data: COVID Tracking Project, http://covidtracking.com | Graph: @kjhealy")
)
ggsave("figures/p_state_casetrend.png", p_state_cases, width = 9, height = 9)
us_states %>%
filter(measure == "death", date == max(date)) %>%
drop_na() %>%
ungroup() %>%
arrange(desc(count))
us_states %>%
group_by(state) %>%
mutate(core = case_when(state %nin% top_n_states(us_states, measure = "death") ~ "",
TRUE ~ state),
end_label = ifelse(date == max(date), core, NA)) %>%
arrange(date) %>%
filter(measure == "death", date > "2020-03-09") %>%
ggplot(aes(x = date, y = count, group = state, color = core, label = end_label)) +
geom_line(size = 0.5) +
geom_text_repel(segment.color = NA, nudge_x = 0.2, nudge_y = 0.1) +
scale_color_manual(values = state_cols) +
scale_x_date(date_breaks = "3 days", date_labels = "%b %e" ) +
scale_y_continuous(trans = "log2",
labels = scales::comma_format(accuracy = 1),
breaks = 2^c(seq(1, 17, 1))) +
guides(color = FALSE) +
coord_equal() +
labs(title = "COVID-19 Cumulative Recorded Deaths by US State",
subtitle = paste("Data as of", format(max(us_states$date), "%A, %B %e, %Y")),
x = "Date", y = "Count of Deaths (log 2 scale)",
caption = "Data: COVID Tracking Project, http://covidtracking.com | Graph: @kjhealy")
```