/
index.Rmd
170 lines (142 loc) · 5.59 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
---
title: "Home"
site: workflowr::wflow_site
output:
workflowr::wflow_html:
toc: false
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = F)
library(tidyverse)
library(lubridate)
library(scales)
library(tidyquant)
library(glue)
library(ckanr) # library to interact with CA Gov. Public Health Portal
theme_set(theme_bw())
# 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)
# get resource ids
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)
counties_of_interest <- c("San Diego", "Los Angeles", "Orange", "Alameda", "Santa Clara")
```
```{r tidy data, message=FALSE}
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)
```
```{r ICU Availability}
ma_n <- 14
per_n_people <- 1e5
hosp_tidy %>%
filter(name == "icu_available_beds") %>%
ggplot(aes(date, value / population * per_n_people, group = county, color = county)) +
geom_ma(linetype = "solid", ma_fun = SMA, n = ma_n) +
ggtitle(glue("ICU Beds Available per {comma(per_n_people)} People"),
subtitle = glue("{ma_n} Day Moving Average")) +
scale_color_discrete(name = "County") +
scale_x_date(name = "Date",
breaks = "14 day",
date_labels = "%b %d") +
scale_y_continuous(name = NULL,
label = comma) +
coord_x_date(xlim = c(min(hosp_tidy$date) + ma_n, NA))
```
```{r COVID ICU Occupancy}
per_n_people <- 1e5
hosp_tidy %>%
filter(name %in% c("icu_covid_confirmed_patients", "icu_suspected_covid_patients")) %>%
group_by(county, date, population) %>%
summarize(value = sum(value)) %>%
ggplot(aes(date, value / population * per_n_people, group = county, color = county)) +
geom_ma(linetype = "solid", ma_fun = SMA, n = ma_n) +
ggtitle(glue("ICU Patients with COVID-19 per {comma(per_n_people)} People"),
subtitle = glue("{ma_n} Day Moving Average")) +
scale_color_discrete(name = "County") +
scale_x_date(name = "Date",
breaks = "14 day",
date_labels = "%b %d") +
scale_y_continuous(name = NULL,
label = comma) +
coord_x_date(xlim = c(min(filter(hosp_tidy, name == "hospitalized_covid_patients")$date) + ma_n, NA))
```
```{r Covid Hospitalizations}
per_n_people <- 1e5
hosp_tidy %>%
filter(name == "hospitalized_covid_patients") %>%
ggplot(aes(date, value / population * per_n_people, group = county, color = county)) +
geom_ma(linetype = "solid", ma_fun = SMA, n = ma_n) +
ggtitle(glue("Hospitalized Patients with COVID-19 per {comma(per_n_people)} People"),
subtitle = glue("{ma_n} Day Moving Average")) +
scale_color_discrete(name = "County") +
scale_x_date(name = "Date",
breaks = "14 day",
date_labels = "%b %d") +
scale_y_continuous(name = NULL,
label = comma) +
coord_x_date(xlim = c(min(filter(hosp_tidy, name == "hospitalized_covid_patients")$date) + ma_n, NA))
```
```{r Covid Deaths}
per_n_people <- 1e6
cases_tidy %>%
filter(name == "newcountdeaths") %>%
ggplot(aes(date, value / population * per_n_people, group = county, color = county)) +
geom_ma(linetype = "solid", ma_fun = SMA, n = ma_n) +
ggtitle(glue("Daily Deaths due to COVID-19 per {comma(per_n_people)} People"),
subtitle = glue("{ma_n} Day Moving Average")) +
scale_color_discrete(name = "County") +
scale_x_date(name = "Date",
breaks = "14 day",
date_labels = "%b %d") +
scale_y_continuous(name = NULL,
label = comma) +
coord_x_date(xlim = c(min(filter(cases_tidy, name == "newcountdeaths")$date) + ma_n, NA))
```
```{r Covid Cases}
per_n_people <- 1e5
cases_tidy %>%
filter(name == "newcountconfirmed") %>%
ggplot(aes(date, value / population * per_n_people, group = county, color = county)) +
geom_ma(linetype = "solid", ma_fun = SMA, n = ma_n) +
ggtitle(glue("Daily Confirmed COVID-19 Cases per {comma(per_n_people)} People"),
subtitle = glue("{ma_n} Day Moving Average")) +
scale_color_discrete(name = "County") +
scale_x_date(name = "Date",
breaks = "14 day",
date_labels = "%b %d") +
scale_y_continuous(name = NULL,
label = comma) +
coord_x_date(xlim = c(min(filter(cases_tidy, name == "newcountconfirmed")$date) + ma_n, NA))
```