Skip to content

Commit b6b0a6f

Browse files
committed
choropleth link and example
1 parent cab8cae commit b6b0a6f

File tree

1 file changed

+50
-3
lines changed

1 file changed

+50
-3
lines changed

vignettes/epidatr.Rmd

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
---
2-
title: "Get started with `epidatr`"
3-
output: rmarkdown::html_vignette
2+
title: "Get started with epidatr"
3+
output:
4+
rmarkdown::html_vignette:
5+
code_folding: show
46
vignette: >
5-
%\VignetteIndexEntry{Get started with `epidatr`}
7+
%\VignetteIndexEntry{Get started with epidatr}
68
%\VignetteEngine{knitr::rmarkdown}
79
%\VignetteDepends{ggplot2}
810
\usepackage[utf8]{inputenc}
@@ -187,6 +189,51 @@ ggplot(epidata, aes(x = time_value, y = value)) +
187189
)
188190
```
189191

192+
`ggplot2` can also be used to [create choropleths](https://r-graphics.org/recipe-miscgraph-choropleth).
193+
194+
195+
```{r class.source = "fold-hide", out.height="65%"}
196+
library(maps)
197+
198+
# Obtain the most up-to-date version of the smoothed covid-like illness (CLI)
199+
# signal from the COVID-19 Trends and Impact survey for all states on a single day
200+
cli_states <- pub_covidcast(
201+
source = "fb-survey",
202+
signals = "smoothed_cli",
203+
geo_type = "state",
204+
time_type = "day",
205+
geo_values = "*",
206+
time_values = 20210410
207+
)
208+
209+
# Get a mapping of states to longitude/latitude coordinates
210+
states_map <- map_data("state")
211+
212+
# Convert state abbreviations into state names
213+
cli_states <- mutate(
214+
cli_states, state = ifelse(
215+
geo_value == "dc",
216+
"district of columbia",
217+
state.name[match(geo_value, tolower(state.abb))] %>% tolower()
218+
)
219+
)
220+
221+
# Add coordinates for each state
222+
cli_states <- left_join(states_map, cli_states, by = c("region" = "state"))
223+
224+
# Plot
225+
ggplot(cli_states, aes(x = long, y = lat, group = group, fill = value)) +
226+
geom_polygon(colour = "black", linewidth = 0.2) +
227+
coord_map("polyconic") +
228+
labs(
229+
title = "Smoothed CLI from Facebook Survey",
230+
subtitle = "All states, 2021-04-10",
231+
x = "Longitude",
232+
y = "Latitude"
233+
)
234+
```
235+
236+
190237
## Finding locations of interest
191238

192239
Most data is only available for the US. Select endpoints report other countries at the national and/or regional levels. Endpoint descriptions explicitly state when they cover non-US locations.

0 commit comments

Comments
 (0)