-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_targets.R
102 lines (86 loc) · 3.35 KB
/
_targets.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
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
library(targets)
library(tarchetypes)
# Set target-specific options such as packages.
tar_option_set(packages = c("tidyverse", "tidycensus"))
# Load helper funcitons
lapply(list.files("./R", full.names = TRUE, recursive = TRUE), source)
# Set up census api key
## TODO: Replace Sys.getenv("CENSUS_API_KEY) with your census api key string.
## Census api key can be requested via https://api.census.gov/data/key_signup.html
tidycensus::census_api_key(Sys.getenv("CENSUS_API_KEY"))
tar_plan(
# Configuration -----------------------------------------------------------
## TODO: Configure your calculation by replacing the following section
tar_target(year, 2010),
tar_target(state, "AL"),
tar_target(top_lvl, "county"),
tar_target(btm_lvl, "tract"),
tar_target(census_code_total, "P003001"), # 2010 Total
tar_target(census_code_maj, "P003002"), # 2010 Total White (Majority)
tar_target(census_code_min, "P003003"), # 2010 Total Black (Minority)
# Census Data Pull --------------------------------------------------------
# Pull census data following the year, state and levels
# TODO: Check if the code names "P003001/2/3" are for population size for your year
tar_target(top_dat,
get_decennial(
geography=top_lvl,
variables = c(
n_total = census_code_total,
n_majority = census_code_maj,
n_minority = census_code_min
),
year = year, state = state) %>%
prep_tidycensus_data()),
tar_target(btm_dat,
get_decennial(
geography=btm_lvl,
variables = c(
n_total = census_code_total,
n_majority = census_code_maj,
n_minority = census_code_min
),
year = year, state = state)%>%
prep_tidycensus_data()),
# Validate the sum of the btm lvl stats is the top lvl stats
tar_target(validate_data_pull,
validate_by_sum(top_dat, btm_dat)
),
# RS Calculation ----------------------------------------------------------
# Calculate residential segregation measures
tar_target(rs_indices,
calc_RS_indices(top_dat, btm_dat)
),
# Create Map --------------------------------------------------------------
# Plot on a map
tar_target(top_geo_dat,
get_decennial(
geography=top_lvl,
variables = c(
n_total = census_code_total,
n_majority = census_code_maj,
n_minority = census_code_min
),
year = year, state = state,
geometry = TRUE) %>%
rename_all(tolower)
),
tar_target(
rs_map,
top_geo_dat %>% group_by(geoid) %>% slice(1) %>%
select(-c(variable, value)) %>%
ungroup() %>%
full_join(
rs_indices
) %>%
ggplot(aes(fill = rs_dissimilarity)) +
geom_sf(color = NA) +
scale_fill_viridis_c(
name = "Dissimilarity",
option = "magma",
limits = c(0,1)
) +
theme_minimal() +
labs(caption = paste0(year, " ", state, " Residential Segregation Index (Dissimilarity) at ",
top_lvl," level.\n", "Transparent areas mean missing scores"))
)
)