-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.Rmd
219 lines (184 loc) · 6.47 KB
/
report.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
---
title: "Vaccine Report"
author: "Rishi Goutam"
params:
state: NA
vaccine_type: NA
insurance_accepted: NA
walkins_allowed: NA
file_type: NA
df: NA
---
```{r test}
# The `params` object is available in the document.
"Params"
params
"State"
params$state
"Vaccine Type"
params$vaccine_type
"Insurance accepted"
params$insurance_accepted
"Walkins allowed"
params$walkins_allowed
"File type"
params$file_type
"df"
params$df
```
```{r params}
# Filters a user can pass. Default values if we receive "NA"
user_states <- (sort(unique(covid$state)))
if(isTRUE(params$state)) {
user_states <- params$state
}
```
```{r params2}
user_vaccine_types <- c("Moderna", "Janssen", "Pfizer", "Pfizer_child")
if(isTRUE(params$vaccine_type)) {
user_vaccine_types <- params$vaccine_type
}
user_insurance_accepted <- TRUE
if(isTRUE(params$insurance_accepted)) {
user_insurance_accepted <- params$insurance_accepted
}
user_walkins_allowed <- TRUE
if(isTRUE(params$walkins_allowed)){
user_walkins_allows <- params$walkins_allowed
}
df <- covid
if(isTRUE(params$df)) {
df <- params$df
}
```
```{r funcs}
# Get the display name for vaccine
GetDisplayName <- function(vaccine_type) {
switch (vaccine_type,
Pfizer = "Pfizer (12+)",
Pfizer_child = "Pfizer (5-11)",
Moderna = "Moderna (18+)",
Janssen = "Janssen (18+)"
)
}
```
```{r provider_count}
# Number of providers in US - no filters
nrow(df)
# Number of providers by state
df %>%
filter(state %in% user_states) %>%
group_by(state) %>%
summarise(num_providers = n()) %>%
arrange(desc(num_providers)) %>%
ggplot(aes(fct_rev(fct_reorder(state, num_providers)), num_providers)) +
geom_col() +
labs(x = "State/Territory", y = "Number of Vaccine Providers", title = "Number of Vaccine Providers by State") +
theme(plot.title = element_text(hjust = 0.5))
```
```{r vaccine_availability}
# % Availability of user vaccines
covid_availability <- df %>%
filter(state %in% user_states) %>%
mutate(across(user_vaccine_types, ~ (!is.na(.x) & !isFALSE(.x)) )) %>%
select(c(location_guid, state, user_vaccine_types))
# need to wrap vaccine_type into .data[[vaccine_type]]
# see: https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html
# E.g,
# for (var in names(mtcars)) {
# mtcars %>% count(.data[[var]])
# }
for (vaccine_type in user_vaccine_types) {
vaccine_availability <-
covid_availability %>%
group_by(state, .data[[vaccine_type]]) %>%
summarise(vaccine_count = n()) %>%
mutate(vaccine_prop = 100*vaccine_count/sum(vaccine_count)) %>%
filter(.data[[vaccine_type]] == TRUE)
vaccine_name <- GetDisplayName(vaccine_type)
print(
ggplot(data = vaccine_availability, aes(fct_rev(fct_reorder(state, vaccine_prop)), vaccine_prop)) +
geom_col() +
ylim(c(0,100)) +
labs(x = "State/Territory", y = str_c(vaccine_name, " in stock (%)"),
title = str_glue("Percentage of Vaccine Providers having {vaccine_name} in stock by state")) +
theme(plot.title = element_text(hjust = 0.5))
)
}
```
```{r vaccine_availability_side_by_side}
# Do above but we want to see multiple types side by side
vaccine_prop_table <- tibble(state = user_states)
for (vaccine_type in user_vaccine_types) {
prop <- covid_availability %>%
group_by(state, .data[[vaccine_type]]) %>%
summarise(vaccine_count = n()) %>%
mutate(vaccine_prop = 100*vaccine_count/sum(vaccine_count)) %>%
filter(.data[[vaccine_type]] == TRUE)
# see: https://stackoverflow.com/questions/25165197/r-create-new-column-with-name-coming-from-variable
vaccine_prop_table[, vaccine_type] <- prop$vaccine_prop
}
# reshape to long
# see: https://stackoverflow.com/questions/58548522/multiple-variables-in-geom-bar-or-geom-col
vaccine_prop_table <- reshape2::melt(vaccine_prop_table, id.vars = "state")
# Plot the in stock percentages by state
prop_legend_labels <- sapply(user_vaccine_types, GetDisplayName)
vaccine_prop_table %>%
ggplot(aes(x = state, value, fill = variable)) +
geom_col(position = "dodge") +
ylim(c(0,100)) +
labs(x = "State/Territory", y = "Percent in stock",
title = str_glue("Vaccines in stock by State and Vaccine Type"),
fill = "Vaccine Type") +
theme(plot.title = element_text(hjust = 0.5)) +
scale_fill_discrete(labels=prop_legend_labels)
```
```{r insurance_acceptance}
# Get proportion of providers that accept insurance
covid_insurance <- df %>%
filter(state %in% user_states) %>%
select(location_guid, state, insurance_accepted) %>%
mutate(insurance_accepted = ifelse(is.na(insurance_accepted), FALSE, insurance_accepted))
insurance_prop_table <- covid_insurance %>%
group_by(state, insurance_accepted) %>%
summarise(insurance_count = n()) %>%
mutate(insurance_prop = 100*insurance_count/sum(insurance_count)) %>%
filter(insurance_accepted == TRUE) %>%
select(state, insurance_prop)
insurance_prop_table$fips <- fips(insurance_prop_table$state)
plot_usmap(data = insurance_prop_table,
regions = "states",
include = user_states,
values = "insurance_prop",
color = "darkgreen") +
scale_fill_continuous(low = "lightgreen", high = "darkgreen",
name = "Insurance Acceptance Rate (%)", label = scales::comma) +
theme(panel.background = element_rect(colour = "darkgreen")) +
theme(legend.position = "right") +
labs(title = "Insurance Acceptance at Vaccine Providers")
```
```{r walkins_allowed}
# Get proportion of providers that allow walkins in states
# TODO: refactor this with insurance code above as the code is identical
covid_walkins <- df %>%
filter(state %in% user_states) %>%
select(location_guid, state, walkins_accepted) %>%
mutate(walkins_accepted = ifelse(is.na(walkins_accepted), FALSE, walkins_accepted))
walkins_prop_table <- covid_walkins %>%
group_by(state, walkins_accepted) %>%
summarise(walkins_count = n()) %>%
mutate(walkins_prop = 100*walkins_count/sum(walkins_count)) %>%
filter(walkins_accepted == TRUE) %>%
select(state, walkins_prop)
walkins_prop_table$fips <- fips(walkins_prop_table$state)
plot_usmap(data = walkins_prop_table,
regions = "states",
include = user_states,
values = "walkins_prop",
color = "darkblue") +
scale_fill_continuous(low = "lightblue", high = "darkblue",
name = "Walkins Allowed Rate (%)", label = scales::comma) +
theme(panel.background = element_rect(colour = "darkblue")) +
theme(legend.position = "right") +
labs(title = "Walk-Ins Allowed at Vaccine Providers")
```