-
Notifications
You must be signed in to change notification settings - Fork 5
/
r2d3.R
35 lines (25 loc) · 1.23 KB
/
r2d3.R
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
# Packages ----------------------------------------------------------------
library(tidyverse)
library(janitor)
library(albersusa)
library(sf)
library(tigris)
library(r2d3maps)
library(rmapshaper)
# County geometries -------------------------------------------------------
cty_sf <- counties_sf("longlat") # from albersusa package
plot(st_geometry(cty_sf)) # checking geometries, looks good
cty_sf <- ms_simplify(cty_sf) # make the geometries simpler for faster rendering
# Data ---------------------------------------------------------------------
# CDC's social vulnerability index variables: https://svi.cdc.gov/Documents/Data/2018_SVI_Data/SVI2018Documentation.pdf
svi <- read_csv("svi_county.csv") %>%
clean_names() %>% # make everything snake_case
filter(across(where(is.numeric), ~. >= 0)) # because NA's are coded as -Inf
# join data to geometries
cty_sf_joined <- cty_sf %>% geo_join(svi, by_sp = "fips", by_df = "fips")
# make d3 map
d3_map(shape = cty_sf_joined, projection = "Albers") %>%
add_labs(caption = "Viz: Asmae Toumi | Data: CDC") %>%
add_continuous_breaks(var = "rpl_theme1", palette = "Reds") %>%
add_legend(title = "Socioeconomic Vulnerability (percentile)") %>%
add_tooltip("<b>{location}</b>: {rpl_theme1}")