-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.Rmd
245 lines (208 loc) · 8.13 KB
/
index.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
---
title: "COVID-19 Trends by UCI Statistics"
site: workflowr::wflow_site
output:
workflowr::wflow_html:
toc: false
editor_options:
chunk_output_type: console
---
<style type="text/css">
.main-container {
max-width: 1200px;
margin-left: auto;
margin-right: auto;
}
</style>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F, warning = F, message = F, fig.retina = 2)
library(tidyverse)
library(lubridate)
library(scales)
library(TTR)
library(glue)
library(ggtext)
library(ckanr) # library to interact with CA Gov. Public Health Portal
library(cowplot)
# connect to CKAN instance
ckanr_setup(url="https://data.ca.gov")
ckan <- ckanr::src_ckan("https://data.ca.gov")
# get resources
resources <- rbind(resource_search("name:covid-19", as = "table")$results,
resource_search("name:hospitals by county", as = "table")$results)
resource_ids <- list(cases = resources$resource_id[resources$name == "COVID-19 Cases"],
tests = resources$resource_id[resources$name == "COVID-19 Testing"],
hosp = resources$resource_id[resources$name == "Hospitals By County"])
# pull resources into data frames (adds extra cols _id and _full_text)
cases <- tbl(src = ckan$con, from = resource_ids$cases) %>%
as_tibble( )%>%
select(-starts_with("_")) %>%
mutate(date = as.Date(date))
tests <- tbl(src = ckan$con, from = resource_ids$test) %>%
as_tibble() %>%
select(-starts_with("_")) %>%
mutate(date = as.Date(date))
hosp <- tbl(src = ckan$con, from = resource_ids$hosp) %>%
as_tibble() %>%
select(-starts_with("_")) %>%
mutate(date = as.Date(todays_date)) %>%
select(-todays_date)
county_pop <- read_csv("data/county_pop.csv") %>% rename_all(str_to_lower)
# stay at home order start and end dates
sah_start <- ymd("2020-03-19")
sah_end <- ymd("2020-05-04")
```
```{r tidy data, message=FALSE}
# Data Processing Options
counties_of_interest <- c("San Diego", "Los Angeles", "Orange", "Alameda", "Santa Clara")
per_n_people <- 1e6 # denominator for reporting counts (e.g, per million people)
ma_n <- 7 # moving average window length in days
# Tidy Data
hosp_tidy <- hosp %>%
filter(county %in% counties_of_interest) %>%
pivot_longer(cols = -c(date, county)) %>%
drop_na() %>%
left_join(county_pop) %>%
arrange(county, date)
cases_tidy <- cases %>%
filter(county %in% counties_of_interest) %>%
pivot_longer(cols = -c(date, county)) %>%
drop_na() %>%
left_join(county_pop) %>%
arrange(county, date)
# Theme options
theme_set(theme_bw(base_size = 14) +
theme(legend.position = "bottom",
legend.title = element_text(size = 12)))
cbPalette <- c("#56B4E9", "#009E73", "#E69F00", "#CC79A7", "#0072B2")
sah_alpha <- 0.2
gglayers = list(
geom_line(size = 1.5),
scale_color_manual(name = "County", values = cbPalette),
scale_x_date(name = "Date",
breaks = "14 day",
date_labels = "%b %d",
expand = expansion(add=c(0,7))),
scale_y_continuous(label = comma)
)
watermark <- function(plt) ggdraw(plt) + draw_label("UC Irvine COVID Awareness Group", color = "#0064A4", alpha=0.4, size = 15, angle = 0, x=0.20, y=0.85, hjust = 0, vjust = 0)
# Could also use stamp function
# stamp(plot1, label = "DRAFT", color = "red")
```
```{r make plots}
# Hospitalizations
hospitalizations_plot_data <- hosp_tidy %>%
filter(name == "hospitalized_covid_patients") %>%
group_by(county) %>%
mutate(value = runMean(value, ma_n)) %>%
drop_na()
hospitalizations_plot <- hospitalizations_plot_data %>%
ggplot(aes(date, value / population * per_n_people, group = county, color = county)) +
ggtitle(glue("Hospitalized Patients with COVID-19 <span style='font-size:14pt'>({ma_n} Day Moving Average)</span>")) +
#subtitle = glue("{ma_n} Day Moving Average"))
theme(
plot.title = element_markdown()
) +
gglayers +
ylab(glue("Hospitalized Patients with COVID-19\nper {comma(per_n_people)} People")) +
annotate("rect",
xmin = if_else(min(hospitalizations_plot_data$date) < sah_start,
sah_start,
min(hospitalizations_plot_data$date)),
xmax = sah_end, ymin = -Inf, ymax = Inf, alpha = sah_alpha) +
annotate("text", y = 175, x = sah_end+8, label = "Stay at Home\nOrder Ended")
# ICU Occupancy
icu_plot_data <- hosp_tidy %>%
filter(name %in% c("icu_covid_confirmed_patients", "icu_suspected_covid_patients")) %>%
group_by(county, date, population) %>%
summarize(value = sum(value)) %>%
group_by(county) %>%
mutate(value = runMean(value, ma_n)) %>%
drop_na() %>%
filter(date >= min(hospitalizations_plot_data$date))
icu_plot <- icu_plot_data %>%
ggplot(aes(date, value / population * per_n_people, group = county, color = county)) +
ggtitle(glue("ICU Patients with COVID-19 <span style='font-size:14pt'>({ma_n} Day Moving Average)</span>")) +
# subtitle = glue("{ma_n} Day Moving Average")) +
theme(
plot.title = element_markdown()
) +
gglayers +
ylab(glue("ICU Patients with COVID-19\nper {comma(per_n_people)} People")) +
annotate("rect",
xmin = if_else(min(icu_plot_data$date) < sah_start, sah_start, min(icu_plot_data$date)),
xmax = sah_end, ymin = -Inf, ymax = Inf, alpha = sah_alpha) +
annotate("text", y = 51, x = sah_end+8, label = "Stay at Home\nOrder Ended")
# Deaths
deaths_plot_data <- cases_tidy %>%
filter(name == "newcountdeaths") %>%
group_by(county) %>%
mutate(value = runMean(value, ma_n)) %>%
drop_na() %>%
filter(date >= min(hospitalizations_plot_data$date))
deaths_plot <- deaths_plot_data %>%
ggplot(aes(date, value / population * per_n_people, group = county, color = county)) +
ggtitle(glue(" New Daily Deaths due to COVID-19 <span style='font-size:14pt'>({ma_n} Day Moving Average)</span>")) +
# subtitle = glue("{ma_n} Day Moving Average")) +
theme(
plot.title = element_markdown()
) +
gglayers +
ylab(glue("New Daily Deaths due to COVID-19\nper {comma(per_n_people)} People")) +
annotate("rect",
xmin = if_else(min(deaths_plot_data$date) < sah_start,
sah_start,
min(deaths_plot_data$date)),
xmax = sah_end, ymin = -Inf, ymax = Inf, alpha = sah_alpha) +
annotate("text", y = 3, x = sah_end+8, label = "Stay at Home\nOrder Ended")
# Cases
cases_plot_data <- cases_tidy %>%
filter(name == "newcountconfirmed") %>%
group_by(county) %>%
mutate(value = runMean(value, ma_n)) %>%
drop_na() %>%
filter(date >= min(hospitalizations_plot_data$date))
cases_plot <- cases_plot_data %>%
ggplot(aes(date, value / population * per_n_people, group = county, color = county)) +
ggtitle(glue("New Daily COVID-19 Cases <span style='font-size:14pt'>({ma_n} Day Moving Average)</span>")) +
# subtitle = glue("{ma_n} Day Moving Average")) +
theme(
plot.title = element_markdown()
) +
gglayers +
ylab(glue("New Daily Confirmed COVID-19 Cases\nper {comma(per_n_people)} People")) +
annotate("rect",
xmin = if_else(min(cases_plot_data$date) < sah_start,
sah_start,
min(cases_plot_data$date)),
xmax = sah_end, ymin = -Inf, ymax = Inf, alpha = sah_alpha) +
annotate("text", y = 250, x = sah_end+8, label = "Stay at Home\nOrder Ended")
```
<div class = "row">
<div class = "col-md-6">
```{r hospitalizations_plot}
watermark(hospitalizations_plot)
```
</div>
<div class = "col-md-6">
```{r icu_plot}
watermark(icu_plot)
```
</div>
</div>
<div class = "row">
<div class = "col-md-6">
```{r cases_plot}
watermark(cases_plot)
```
</div>
<div class = "col-md-6">
```{r deaths_plot}
watermark(deaths_plot)
```
</div>
</div>
Data collected from the [California Open Data Portal](https://data.ca.gov/group/covid-19).
Last updated on `r Sys.Date()`
Last available date in the [California Open Data Portal](https://data.ca.gov/group/covid-19) is `r as.Date(max(hosp_tidy$date, cases_tidy$date))`.
<!-- Last available date in the [California Open Data Portal](https://data.ca.gov/group/covid-19) is `r as.Date(max(resources$last_modified[resources$name %in% c("COVID-19 Cases", "COVID-19 Testing", "Hospitals By County")]))`. -->