forked from GeospatialCentroid/dsri_foco_habicon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_prep.Rmd
189 lines (164 loc) · 5.67 KB
/
app_prep.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
# Prep Data for Shiny App
```{r}
source("setup.R")
```
## Priority Maps
Create species richness maps
```{r}
# patch priority
patch_maps <- map(
list.files(
"data/output_habicon/",
pattern = "patch_priority.tif",
full.names = TRUE
),
terra::rast
)
## rescale values to sum all together
rescale_function <- function(x) {
map(names(x), ~ x[[.x]] %>% habicon::rescale_map()) %>%
do.call(c, .)
}
patch_rescale <- map(patch_maps, rescale_function) %>%
# reduce to single spatraster
do.call(c, .)
patch_sum <- map(c("qwa", "btwn", "dECA"), function(x) {
r <- subset(patch_rescale, grep(x, names(patch_rescale))) %>%
sum(na.rm = TRUE)
names(r) <- x
return(r)
}) %>% do.call(c, .)
# corridor priority
corr_maps <- map(
list.files(
"data/output_habicon/",
pattern = "corridor_priority.tif",
full.names = TRUE
),
terra::rast
)
## rescale values and sum all together
corr_sum <- map(corr_maps, habicon::rescale_map) %>%
# reduce to single spatraster
do.call(c, .) %>%
sum(na.rm = TRUE)
# save
writeRaster(patch_sum, "data/output_habicon/patch_priority_all.tif", overwrite = TRUE)
writeRaster(patch_sum, "shiny/data/output_habicon/patch_priority_all.tif", overwrite = TRUE)
writeRaster(corr_sum, "data/output_habicon/corridor_priority_all.tif", overwrite = TRUE)
writeRaster(corr_sum, "shiny/data/output_habicon/corridor_priority_all.tif", overwrite = TRUE)
```
## Census Data
Define variables to pull
```{r}
# pull the datasets
acs <- tidycensus::get_acs(
geography = "block group",
state = "CO",
county = "Larimer",
geometry = TRUE,
variables = c(
# under 5
"B01001_003",
"B01001_027",
# over 64
paste0("B01001_0", 20:25),
paste0("B01001_0", 44:49),
#percent people of color
"B03002_001",
"B03002_003",
#Percent low income
"C17002_001",
"C17002_008",
# Median household income
"B19013_001",
#Percent linguistic isolation
"C16002_001",
"C16002_004",
"C16002_007",
"C16002_010",
"C16002_013",
#Percent less than high school education
"B15002_001",
paste0("B15002_00", 2:9),
"B15002_010",
paste0("B15002_0", 20:27),
#Percent disability
paste0("B18101_", c("001","004","007","010","013","016","019","023",
"026","029","032","035","038")),
#total Population
"B01003_001",
# lead housing
"B25034_001", # total housing units
"B25034_009", # 1950-1959
"B25034_010", # 1940-1949
"B25034_011", # pre 1939
# housing burden
"B25070_001", # Total Renters
"B25070_007", # 30 to 34.9%
"B25070_008", # 35 to 39.9%
"B25070_009", # 40 to 49.9%
"B25070_010", # 50% or more
"B25091_001", # total owner-occupied,
# "B25003_002", # confirmation of previous var - total owner occupied,
"B25091_008", # 30 to 34.9% - mortgaged
"B25091_009", # 35 to 39.9% - mortgaged
"B25091_010", # 40 to 49.9% - mortgaged
"B25091_011", # 50% or more - mortgaged
"B25091_019", # 30 to 34.9% - not mortgaged
"B25091_020", # 35 to 39.9% - not mortgaged
"B25091_021", # 40 to 49.9% - not mortgaged
"B25091_022" # 50% or more - not mortgaged
)
)
```
### Calculate, clean and save final ACS data
```{r}
cleaned_acs <- acs %>%
select(GEOID, NAME, variable, estimate) %>%
pivot_wider(names_from = variable, values_from = estimate) %>%
# calculate variables
group_by(GEOID) %>%
mutate(
# HOUSING BURDEN
HHUnits = B25070_001+B25091_001, # renter total + owner total
housing_burden = B25070_007+B25070_008+B25070_009+B25070_010+
B25091_008+B25091_009+B25091_010+B25091_011+
B25091_019+B25091_020+B25091_021+B25091_022, # >30% renters, mortgaged, nonmortgaged
percent_housing_burden = housing_burden/HHUnits * 100,
# PERCENT DISABILITY
disability = sum(B18101_004, B18101_007,
B18101_010, B18101_013,
B18101_016, B18101_019,
B18101_023, B18101_026,
B18101_029, B18101_032,
B18101_035, B18101_038 ),
percent_disability = sum(B18101_004, B18101_007,
B18101_010, B18101_013,
B18101_016, B18101_019,
B18101_023, B18101_026,
B18101_029, B18101_032,
B18101_035, B18101_038 ) / B18101_001,
# PEOPLE OF COLOR
racial_minority = ifelse(B03002_001 == 0,
NA,
B03002_001 - B03002_003),
percent_racial_minority = ifelse(B03002_001 == 0,
NA,
(B03002_001 - B03002_003) /
B03002_001 * 100),
# LOW INCOME
low_income = ifelse(C17002_001 == 0, NA, C17002_001 - C17002_008),
percent_low_income = ifelse(C17002_001 == 0, NA, (C17002_001 - C17002_008) / C17002_001 * 100),
# population density
area_sq_km = as.numeric(st_area(geometry)) / 1e6, # convert to sq km
population_density = B01003_001 / area_sq_km
) %>%
dplyr::select(GEOID, NAME, total_population = B01003_001, median_income = B19013_001, housing_burden:population_density)
#reproject for leaflet
cleaned_acs <- st_transform(cleaned_acs, 4326)
st_write(cleaned_acs, "shiny/data/acs_Larimer_2022.shp", append = FALSE)
# also save as .RDS to preserve column names for shiny app
save(cleaned_acs, file = "shiny/data/acs_Larimer_2022.RData")
```
*No disability data for Larimer County*