-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 21.4 KB
/
.Rhistory
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
inner_join(pct_female_headed_households, by="neighborhood")
top_pcts <- distinct(top_10 %>%
select(neighborhood)) %>%
inner_join(female_cmbd, by = "neighborhood") %>%
select(femhhs21)
mean(top_pcts$femhhs21)
bottom_pcts <- distinct(bottom_10 %>%
select(neighborhood)) %>%
inner_join(female_cmbd, by = "neighborhood") %>%
select(femhhs21)
mean(bottom_pcts$femhhs21)
# As the code above shows, the percentage of female-headed households in Baltimore’s top 10% wealthiest neighborhoods is 22.06%. The percentage of female-headed households in Baltimore’s bottom 10% wealthiest neighborhoods is 66.82%.
#C - load Lobraries
library(tidyverse)
library(rio)
library(janitor)
#intro
#For my notebook I wanted to test if there was correlation between race and housing prices in Baltimore City. In the data sets we have looked at previously we were able to identify regional disparities in median income and race. We have also analyzed population ethnic percentages in these areas. The missing piece that could tie the data notebook together would be the housing prices data. With the regional data median housing price data of Baltimore we can compare the housing value and race population. This would indicate the value of the neighborhood and we can see if there are trends with neighborhood value, race and income.
#this is the Median housing data of cities in Baltimore MD. I will create table to assess which cities in MD had the highest and lowest value of homes and then assess my findings with the other data. I will be looking at the home value of the most current year in each city. So i will sort the data according to the 2019 home value median price.
Median_Price_of_Homes_Sold <- read_csv("~/Documents/GitHub/abbey_weltman_jour472/abbeyweltman_notebook/Median_Price_of_Homes_Sold.csv")
Median_Price_of_Homes_Sold <- read_csv("Median_Price_of_Homes_Sold.csv")
high_home_pr <- Median_Price_of_Homes_Sold %>%
select(CSA2010,salepr19) %>%
arrange(desc(salepr19)) %>%
slice_max(salepr19, n = 5)
low_home_pr <-Median_Price_of_Homes_Sold %>%
select(CSA2010,salepr19) %>%
arrange(salepr19) %>%
slice_max(salepr19, n = 5)
View(Median_Price_of_Homes_Sold)
View(high_home_pr)
View(low_home_pr)
View(high_home_pr)
View(low_home_pr)
View(high_home_pr)
Racial_Diversity_Index_1_ <- read_csv("Racial_Diversity_Index (1).csv")
diverse_CSA <- Racial_Diversity_Index_1_ %>%
filter(CSA2020 == c("North Baltimore/Guilford/Homeland")) %>%
select(racdiv21,CSA2020) %>%
arrange(desc(racdiv21)) %>%
slice_max(racdiv21, n = 10)
View(diverse_CSA)
diverse_CSA <- Racial_Diversity_Index_1_ %>%
filter(CSA2020 == c("North Baltimore/Guilford/Homeland"))
city_md_income <- read_csv("city_md_income.csv")
top_income <- city_md_income %>%
select(place ,median_inc_2020)%>%
arrange(desc(median_inc_2020)) %>%
slice_max(median_inc_2020, n = 20)
low_income <- city_md_income %>%
select(place, median_inc_2020)%>%
arrange(median_inc_2020) %>%
slice_max(median_inc_2020, n = 20)
View(top_income)
View(low_income)
View(top_income)
View(low_income)
#remotes::install_github("walkerke/tidycensus")
library(tidyverse)
library(tidycensus)
#a = get_decennial(geography = "state", variables = "P1_001N", year = 2020)
# head(a, 5)
#install.packages("formattable")
library(formattable)
census_api_key("9cabe8a191a1f824755d4a1845f13cb08faa2c5f", install = TRUE)
#2020 census tract
#P1_003N !!Total:!!Population of one race:!!White alone
race_dec <- tidycensus::get_decennial( geography = "tract", geometry = TRUE, state = "MD", year = 2020, variables = "P1_003N", summary_var = "P1_002N")
#Calls variables for the 2020 decennial census
v20 <- load_variables(2020, "pl", cache = TRUE)
v16 <- load_variables(2016, "acs5", cache = TRUE)
v19 <- load_variables(2019, "acs5", cache = TRUE)
#vars10 <- c("P005003", "P005004", "P005006", "P004003")
# P1_003N
# !!Total:!!Population of one race:!!White alone
# P1_004N
# !!Total:!!Population of one race:!!Black or African American alone
# P1_005N
# !!Total:!!Population of one race:!!American Indian and Alaska Native alone
# P1_006N
# !!Total:!!Population of one race:!!Asian alone
# P2_002N
# !!Total:!!Hispanic or Latino
vars20 <- c("P1_003N", "P1_004N", "P1_005N", "P1_006N", "P2_002N")
#Race by Maryland County, 2020
MD_race <- get_decennial(geography = "county", geometry = TRUE, state = "MD", year = 2020, variables = c(white = "P1_003N", black = "P1_004N", asian = "P1_006N", nativeam = "P1_005N", hispanic = "P2_002N"), summary_var = "P1_002N") %>%
mutate(pct_race = (value / summary_value)) %>%
rename(race = variable, population = value, county_pop = summary_value)
MD_race$pct_race <- round(MD_race$pct_race, 2)
MD_race$pct_race <- percent(MD_race$pct_race, 0)
#using the rename variables feature
#https://walker-data.com/census-r/an-introduction-to-tidycensus.html#renaming-variable-ids
MD_race <- as.data.frame(MD_race)
MD_race <- MD_race %>%
select(NAME, race, pct_race, population, county_pop)
#write.csv (MD_race, "MD_2020_counties_race.csv")
#B19001 COUNTS THE NUMBER OF HOUSEHOLDS
md_income1 <- get_acs(geography = "county",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2016)
md_income1
#B19001 COUNTS THE NUMBER OF HOUSEHOLDS
md_income2016 <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2016) %>%
mutate(year=("2016"))
md_income2016
#B19001 COUNTS THE NUMBER OF HOUSEHOLDS
md_income2020 <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2020) %>%
mutate(year=("2020"))
md_income2020
#B19001 COUNTS THE NUMBER OF HOUSEHOLDS
md_income2010 <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2010) %>%
mutate(year=("2010"))
md_income2010
md_income_all <- rbind(md_income2010, md_income2016, md_income2020)
md_income_all <- separate(data = md_income_all, col = NAME, into = c("Census_Tract", "County", "State"), sep = ",", extra = "merge", fill = "right")
#write.csv(md_income_all, "md_income_all.csv")
balt_income_all <- filter(md_income_all, grepl ("Baltimore", County))
#write.csv(balt_income_all, "balt_income_all.csv")
baltcity_income <- filter(balt_income_all, grepl ("Baltimore city", County))
baltcity_income <- baltcity_income %>%
filter(variable=="median_income")
View(baltcity_income)
# fish_encounters %>%
# pivot_wider(names_from = station, values_from = seen)
baltcity_income2 <- baltcity_income %>%
select(!(moe)) %>%
pivot_wider(names_from = year, values_from = estimate)
baltcity_income2 <- janitor::clean_names(baltcity_income2)
baltcity_income2 <- baltcity_income2 %>%
mutate(Diff_2020_2010 = (x2020-x2010)) %>%
mutate(Diff_2020_2016 = (x2020-x2016))
# write.csv(baltcity_income2, "baltcity_income2.csv")
# write_rds(baltcity_income2, "baltcity_income2.rds")
state_list <- fips_codes %>%
distinct(state) %>%
as.list()
state_list <-state_list$state[1:51]
decennial_vars <- load_variables(2020, dataset="pl")
nation_geometries <- get_decennial(year=2020, geography="tract",state=state_list, variables = "P3_001N", sumfile = "pl", geometry = TRUE)
clean_dec_nation_geometries <- nation_geometries %>%
clean_names() %>%
separate(name, into = c("tract","county","state"), sep=", ") %>%
mutate(total_pop=value) %>%
select(-variable,-value)
clean_dec_nation_geometries <- nation_geometries %>%
janitor::clean_names() %>%
separate(name, into = c("tract","county","state"), sep=", ") %>%
mutate(total_pop=value) %>%
select(-variable,-value)
baltcity_income_map <- nation_geometries %>%
right_join(baltcity_income2, by=c("GEOID"="geoid"))
View(baltcity_income_map)
clean_dec_nation_geometries <- readRDS("dec_nation_geometries.rds")
clean_dec_nation_geometries <- readRDS("data\dec_nation_geometries.rds")
clean_dec_nation_geometries <- readRDS("..\data\dec_nation_geometries.rds")
getwd()
setwd("~/Code/Baltimore/code")
clean_dec_nation_geometries <- readRDS("..\data\dec_nation_geometries.rds")
clean_dec_nation_geometries <- readRDS("data\dec_nation_geometries.rds")
clean_dec_nation_geometries <- readRDS("../Baltimore/data/dec_nation_geometries.rds")
getwd()
setwd("~/Code/Baltimore")
clean_dec_nation_geometries <- readRDS("../Baltimore/data/dec_nation_geometries.rds")
clean_dec_nation_geometries <- readRDS("/Users/robwells/Code/Baltimore/data/dec_nation_geometries.rds")
MD_dec_geometries <- clean_dec_nation_geometries %>%
filter(county== "Baltimore city")
pal <- colorNumeric(
palette = "inferno",
domain = MD_dec_geometries$total_pop
)
library(leaflet)
MD_dec_geometries <- clean_dec_nation_geometries %>%
filter(county== "Baltimore city")
pal <- colorNumeric(
palette = "inferno",
domain = MD_dec_geometries$total_pop
)
leaflet() %>%
addProviderTiles(providers$Stamen.TonerLite) %>%
addPolygons(data = MD_dec_geometries,
color = ~pal(total_pop),
weight = 0.5,
smoothFactor = 0.2,
fillOpacity = 0.75,
label = ~total_pop) %>%
addLegend(
position = "bottomright",
pal = pal,
values = MD_dec_geometries$total_pop,
title = "total pop <br>per census tract"
)
#remotes::install_github("walkerke/tidycensus")
library(tidyverse)
library(tidycensus)
#a = get_decennial(geography = "state", variables = "P1_001N", year = 2020)
# head(a, 5)
#install.packages("formattable")
library(formattable)
library(htmlwidgets)
library(leaflet)
library(sf)
library(formattable)
library(dplyr)
library(tidyr)
library(janitor)
#install.packages("leaflet.extras")
library(leaflet.extras)
library(googlesheets4)
census_api_key("9cabe8a191a1f824755d4a1845f13cb08faa2c5f", install = TRUE, overwrite = TRUE)
googlesheets4::gs4_deauth()
smith <- read_sheet("https://docs.google.com/spreadsheets/d/1RheKDdtsToCcSLnZoZBYwtN_ISw6E2kgPIQpDQE_BYw/edit?usp=sharing") %>%
as.data.frame()
smith <- smith %>%
clean_names()
#join Community Statistical Area names
#associates neighborhood names to Census Tracts using Community Statistical Areas, 2010 https://bniajfi.org/mapping-resources/
#Baltimore has 56 neighborhoods as measured by Community Statistical Areas
csa <- read_csv("../data/balt_census_crosswalks_2020.csv") %>%
rename(
geoid = GEOID_Tract_2020
) %>%
clean_names()
#smith$tract <- as.character(smith$tract)
smith1 <- smith %>%
right_join(csa, by=c("tract"="geoid")) %>%
distinct()
#Rename Columns
smith1<- smith1 %>%
rename(neighborhood = community_statistical_area_2020,
census = tract_2020)
#write.csv(smith1, "smith1_balt.csv")
smith <- read.csv("../data/smith1_balt.csv")
#199 census tracts
#25 zip codes (totalling 199 areas, but need to check boundaries)
x<- smith %>%
group_by(zip) %>%
count ()
sum(x$n)
#per capita income seems way too low
median(smith$income_per_capita, na.rm = TRUE) #22789.76
median(smith$pct_anymembershp_zip, na.rm = TRUE) #0.455563
summary(smith$pct_anymembershp_zip, na.rm = TRUE)
# Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
# 0.3353 0.4444 0.4556 0.4613 0.4752 0.5241 3
median(smith$nbanks_zip, na.rm = TRUE) #5
summary(smith$nbanks_zip, na.rm = TRUE)
# Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
# 0.000 2.000 5.000 5.847 8.000 14.000 3
median(smith$census_response_rate2020, na.rm = TRUE) #0.564
summary(smith$census_response_rate2020, na.rm = TRUE)
# Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
# 0.2830 0.4753 0.5640 0.5594 0.6522 0.8360 3
baltcity_income_clean <- read.csv("baltcity_income_clean.csv")
baltcity_income_clean <- read.csv("data/baltcity_income_clean.csv")
getwd()
baltcity_income_clean <- read.csv("../data/baltcity_income_clean.csv")
baltcity_percap_income <- read_csv("../data/baltcity_percap_income.csv") %>%
as.data.frame()
median(baltcity_income_clean$x2020, na.rm = TRUE)
baltcity_percap_income <- read_csv("../data/baltcity_percap_income.csv") %>%
as.data.frame()
median(baltcity_percap_income$x2020, na.rm = TRUE)
#Calls variables for the 2020 decennial census
v20_dec <- load_variables(2020, "pl", cache = TRUE)
#Calls variables for the 2020 ACS census
v20_acs <- load_variables(2020, "acs5", cache = TRUE)
#2020 Median Income By Census Tract ACS
#2016-2020
#B19001 COUNTS THE NUMBER OF HOUSEHOLDS
#B19013_001 is the household median income
md_income2020 <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2020) %>%
mutate(year=("2020"))
md_income2020
#B19013 defined: https://www.socialexplorer.com/data/ACS2010_5yr/metadata/?ds=ACS10_5yr&var=B19013001
#2016 Median Income By Census Tract ACS
#2016-2012
md_income2016 <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2016) %>%
mutate(year=("2016"))
md_income2016
#2010 Median Income By Census Tract 2010
#2006-2010 sample
md_income2010 <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2010) %>%
mutate(year=("2010"))
md_income2010
#Bind together 2010, 2016, 2020 files
md_income_all <- rbind(md_income2010, md_income2016, md_income2020)
md_income_all <- separate(data = md_income_all, col = NAME, into = c("Census_Tract", "County", "State"), sep = ",", extra = "merge", fill = "right")
#write.csv(md_income_all, "md_income_all.csv")
#Subset Baltimore city-county, city files
balt_income_all <- filter(md_income_all, grepl ("Baltimore", County))
#write.csv(balt_income_all, "balt_income_all.csv")
#Baltimore city median income, 2010, 2016, 2020
baltcity_income <- filter(balt_income_all, grepl ("Baltimore city", County)) %>%
filter(variable=="median_income")
#reshape baltcity_income table
baltcity_income <- baltcity_income %>%
dplyr::select(-moe) %>%
pivot_wider(names_from = "year", values_from = "estimate")
baltcity_income <- janitor::clean_names(baltcity_income)
baltcity_income <- baltcity_income %>%
mutate(diff_2020_2010 = (x2020-x2010)) %>%
mutate(diff_2020_2016 = (x2020-x2016))
# write.csv(baltcity_income, "baltcity_income.csv")
# write_rds(baltcity_income, "baltcity_income.rds")
member_all <- smith %>%
group_by(neighborhood) %>%
select(pct_anymembershp_zip, income_per_capita, tract) %>%
arrange(desc(pct_anymembershp_zip)) %>%
mutate(pct_anymembershp_zip = formattable::percent(pct_anymembershp_zip))
member_all$income_per_capita <- formattable::currency(member_all$income_per_capita, digits =0L)
library(kableExtra)
member_all %>%
rename(Neighborhood = neighborhood, Membership = pct_anymembershp_zip, Per_Capita_Income = income_per_capita, Census_Tract = tract) %>%
kbl(caption = "All Communities By Civic Membership", font_size = 50, bold=T) %>%
kable_classic(full_width = T, html_font = "Cambria") %>%
column_spec(1, bold = T, border_right = T) %>%
column_spec(2, border_right = T, width = "10em", background = "yellow") %>%
column_spec(3, border_right = T, width = "10em") %>%
save_kable(file = "../output/member_all.html", self_contained = T)
#subset the geometry
md_income2020_geo <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2020, geometry = TRUE) %>%
mutate(year=("2020")) %>%
select(GEOID, NAME)
member_all <- member_all %>%
mutate(GEOID = as.character(tract))
#join geocoordinates with baltcity
member_all2 <- md_income2020_geo %>%
right_join(member_all, by=c("GEOID")) %>%
distinct()
member_all2$pct_anymembershp_zip <- formattable::percent(member_all2$pct_anymembershp_zip, 1)
# member_all2$income_per_capita <-
# paste0('$', round(member_all2$income_per_capita,0))
View(member_all2)
View(smith1)
View(md_income2020_geo)
View(member_all)
md_income2020_geo <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2020, geometry = TRUE) %>%
mutate(year=("2020"))
View(baltcity_income_clean)
View(baltcity_income)
View(baltcity_percap_income)
md_income2022_geo <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2022, geometry = TRUE) %>%
mutate(year=("2022")) %>%
select(GEOID, NAME)
View(md_income2022_geo)
md_income2022_geo <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2022, geometry = TRUE) %>%
mutate(year=("2022"))
member_all <- member_all %>%
mutate(GEOID = as.character(tract))
md_income2022_geo <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2022, geometry = TRUE) %>%
mutate(year=("2022")) %>%
rename(hh_med_inc_2022 = estimate)
member_all2 <- md_income2022_geo %>%
right_join(member_all, by=c("GEOID")) %>%
distinct()
md_income2022_geo <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2022, geometry = TRUE) %>%
mutate(year=("2022")) %>%
rename(hh_med_inc_2022 = estimate) %>%
filter(viable="median_income")
md_income2022_geo <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2022, geometry = TRUE) %>%
mutate(year=("2022")) %>%
rename(hh_med_inc_2022 = estimate) %>%
filter(viable=="median_income")
md_income2022_geo <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2022, geometry = TRUE) %>%
mutate(year=("2022")) %>%
rename(hh_med_inc_2022 = estimate) %>%
filter(variable=="median_income")
member_all2 <- md_income2022_geo %>%
right_join(member_all, by=c("GEOID")) %>%
distinct()
member_all2$pct_anymembershp_zip <- formattable::percent(member_all2$pct_anymembershp_zip, 1)
#Map by Median Income Difference
member_all2 <- member_all2 %>%
sf::st_transform('+proj=longlat +datum=WGS84')
pal <- colorNumeric(
palette = "inferno",
domain = member_all2$pct_anymembershp_zip
)
#https://leaflet-extras.github.io/leaflet-providers/preview/
leafMap <- leaflet() %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
addPolygons(data = member_all2,
color = ~pal(pct_anymembershp_zip),
weight = 2.5,
smoothFactor = 0.2,
fillOpacity = 0.5,
label = paste("Pct membership in civic groups is:",(member_all2$pct_anymembershp_zip), "and household median income is",(member_all2$hh_med_inc_2022), "in", (member_all2$neighborhood))) %>%
# addLegend(
# position = "bottomright",
# pal = pal,
# values = member_all2$pct_anymembershp_zip,
# title = "Pct Membership <br> 2020"
# ) %>%
addControl(
html = '<div id="legend" style="background-color: white; padding: 10px; border-radius: 5px; border: 1px solid #ccc; text-align: center;">
<p><strong>Pct Membership 2020</strong></p>
<div style="background-color: black; height: 20px; width: 30px; display: inline-block;"></div> 30-39%
<br>
<div style="background-color: #e75480; height: 20px; width: 30px; display: inline-block;"></div> 40-45%
<br>
<div style="background-color: #fa8072; height: 20px; width: 30px; display: inline-block;"></div> 46-50%
<br>
<div style="background-color: #ffffbf; height: 20px; width: 30px; display: inline-block;"></div> 51-60%
</div>',
position = "bottomright"
) %>%
addControl(
html = "<div style='background-color: white; padding: 5px; border-radius: 3px;'><h3>Civil Membership, Household Median Income in Baltimore</h3></div>",
position = "topright"
)
# saveWidget(leafMap, "../output/member_all_map_jan_24.html")
saveWidget(leafMap, "../output/member_all_map_march_6.html")
leafMap
md_income2022_geo <- get_acs(geography = "tract",
variables = c(number_households = "B19001_001", median_income = "B19013_001"),
state = "MD",
year = 2022, geometry = TRUE) %>%
mutate(year=("2022")) %>%
rename(hh_med_inc_2022 = estimate) %>%
filter(variable=="median_income") %>%
mutate(hh_med_inc_2022 = formattable::currency(hh_med_inc_2022, digits =0L))
View(md_income2022_geo)
member_all2 <- md_income2022_geo %>%
right_join(member_all, by=c("GEOID")) %>%
distinct()
member_all2$pct_anymembershp_zip <- formattable::percent(member_all2$pct_anymembershp_zip, 1)
View(member_all2)
#Map by Median Income Difference
member_all2 <- member_all2 %>%
sf::st_transform('+proj=longlat +datum=WGS84')
pal <- colorNumeric(
palette = "inferno",
domain = member_all2$pct_anymembershp_zip
)
#https://leaflet-extras.github.io/leaflet-providers/preview/
leafMap <- leaflet() %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
addPolygons(data = member_all2,
color = ~pal(pct_anymembershp_zip),
weight = 2.5,
smoothFactor = 0.2,
fillOpacity = 0.5,
label = paste("Pct membership in civic groups is:",(member_all2$pct_anymembershp_zip), "and household median income is",(member_all2$hh_med_inc_2022), "in", (member_all2$neighborhood))) %>%
# addLegend(
# position = "bottomright",
# pal = pal,
# values = member_all2$pct_anymembershp_zip,
# title = "Pct Membership <br> 2020"
# ) %>%
addControl(
html = '<div id="legend" style="background-color: white; padding: 10px; border-radius: 5px; border: 1px solid #ccc; text-align: center;">
<p><strong>Pct Membership 2020</strong></p>
<div style="background-color: black; height: 20px; width: 30px; display: inline-block;"></div> 30-39%
<br>
<div style="background-color: #e75480; height: 20px; width: 30px; display: inline-block;"></div> 40-45%
<br>
<div style="background-color: #fa8072; height: 20px; width: 30px; display: inline-block;"></div> 46-50%
<br>
<div style="background-color: #ffffbf; height: 20px; width: 30px; display: inline-block;"></div> 51-60%
</div>',
position = "bottomright"
) %>%
addControl(
html = "<div style='background-color: white; padding: 5px; border-radius: 3px;'><h3>Civil Membership, Household Median Income in Baltimore</h3></div>",
position = "topright"
)
# saveWidget(leafMap, "../output/member_all_map_jan_24.html")
saveWidget(leafMap, "../output/member_all_map_march_6.html")
leafMap