This I borrowed from the class .Rmd files, nothing extra was added.
I had some issues with my census API key, for whatever reason it was saying that it was inactive. This is me making sure that the right key is being used.
census_api_key("8d3c5e8a8dae0475011a9bb5c906d0bb1e6490bf", install=TRUE, overwrite=TRUE)
## [1] "8d3c5e8a8dae0475011a9bb5c906d0bb1e6490bf"
I decided that I wanted to look at Public Assistance Income or Food Stamps/SNAP in the Past 12 months for Households (Table B19058). Because I live in Oakland I decided to keep it local and continue to look at Alameda County.
To make my life easier I’m going to come up with a nicer way to name the variables I want to use. B19058_001 is the total number of households per tract, B19058_002 is the number of households receiving SNAP in the past year, and B19058_003 is the number of households per tract not recieving SNAP. I’m going to call these three variables snap_vars
snap_vars = c("B19058_001","B19058_002","B19058_003")
Now I’ll pull the relevant ACS data.
ac_snap <-get_acs(geography = "tract",
variables = snap_vars,
state = "CA",
county = "Alameda",
year = 2023)
view(ac_snap)
Even though it’s not a ton of variables, ac_snap would still benefit from widening.
wide_ac_snap <-ac_snap %>%
pivot_wider(names_from = variable, values_from = c(estimate, moe))
view(wide_ac_snap)
I’m also going to clean up the column headers, which are unintelligible unless I can manage to remember each variable. THe way I made this selection also got rid of the margin of error columns.
pretty_ac_snap <-wide_ac_snap %>%
dplyr::select(GEOID,NAME,HOUSEHOLDS=estimate_B19058_001, SNAP_RECIPIENT=estimate_B19058_002, NO_SNAP_RECIPIENT = estimate_B19058_003)
view(pretty_ac_snap)
The data that I have is totals, so I’m going to create 2 new columns. One will provide the percent of SNAP recipients households in each tract, the other will provide the percent of households that didn’t receive SNAP.
new_ac_snap <- pretty_ac_snap %>%
mutate(PERCENT_SNAP = (SNAP_RECIPIENT/HOUSEHOLDS)*100, PERCENT_NO_SNAP = (NO_SNAP_RECIPIENT/HOUSEHOLDS)*100)
view(new_ac_snap)
Making a quick map of Alameda County.
ac_geo <-tracts(state= 'CA', county= 'Alameda', progress_bar = FALSE)
qtm(ac_geo)
I want to make sure I can join this to new_ac_snap with ease so I’m going to glimpse and check that there’s a column in common.
glimpse(ac_geo)
## Rows: 379
## Columns: 14
## $ STATEFP <chr> "06", "06", "06", "06", "06", "06", "06", "06", "06", "06", "…
## $ COUNTYFP <chr> "001", "001", "001", "001", "001", "001", "001", "001", "001"…
## $ TRACTCE <chr> "442700", "442800", "442900", "443001", "443002", "443102", "…
## $ GEOID <chr> "06001442700", "06001442800", "06001442900", "06001443001", "…
## $ GEOIDFQ <chr> "1400000US06001442700", "1400000US06001442800", "1400000US060…
## $ NAME <chr> "4427", "4428", "4429", "4430.01", "4430.02", "4431.02", "443…
## $ NAMELSAD <chr> "Census Tract 4427", "Census Tract 4428", "Census Tract 4429"…
## $ MTFCC <chr> "G5020", "G5020", "G5020", "G5020", "G5020", "G5020", "G5020"…
## $ FUNCSTAT <chr> "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "…
## $ ALAND <dbl> 1234016, 1278646, 2066523, 768730, 1580326, 2367415, 1556745,…
## $ AWATER <dbl> 0, 0, 0, 0, 0, 0, 0, 60526, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
## $ INTPTLAT <chr> "+37.5371513", "+37.5293619", "+37.5184093", "+37.5184218", "…
## $ INTPTLON <chr> "-122.0081095", "-121.9931002", "-121.9748369", "-121.9515237…
## $ geometry <MULTIPOLYGON [°]> MULTIPOLYGON (((-122.0172 3..., MULTIPOLYGON (((…
GEOID is a column they both have in common, and there are 379 rows for each so I should be safe to join. I set breaks that end at 40 because the highest value in the column I want to look at “PERCENT_SNAP,” is 40.
test_ac_snap_map <- merge(ac_geo, new_ac_snap, by.x = "GEOID", by.y = "GEOID")
qtm(test_ac_snap_map, "PERCENT_SNAP", fill.style="fixed",fill.breaks=c(0,10,20,30,40),fill.palette="Greens")
## Warning: Values have found that are higher than the highest break. They are
## assigned to the highest interval
This generates a color graded map of the census tracts in Alameda
County, where each tract gets darker as there gets to be a higher
proportion of households that receive SNAP benefits. I do get a warning
that there are values above the highest break, but that’s because
there’s one tract with at 40.340219%, which for the purposes of our
visualization is fine to get included with the 30-40 break.
However, I’m noticing that it kept the water in Alameda County shaded, and I don’t want that to mess with my future mapping, so I’m going try to make the same map but filtering it for only areas where households are greater than 0.
ac_snap_map <- test_ac_snap_map %>%
filter(HOUSEHOLDS > 0)
qtm(ac_snap_map, "PERCENT_SNAP", fill.style="fixed",fill.breaks=c(0,10,20,30,40),fill.palette="Greens")
## Warning: Values have found that are higher than the highest break. They are
## assigned to the highest interval
This gives me a version without the water included. Now I’m going to
make my interactive leaflet map, still making sure I filter out the
water.
ac_snap_map <- merge(ac_geo, new_ac_snap, by.x = "GEOID", by.y = "GEOID",fill.breaks=c(0,10,20,30,40)) %>%
filter(HOUSEHOLDS > 0)
snap_palette <- colorNumeric(palette = "Reds", domain=ac_snap_map$PERCENT_SNAP)
snap_popup <- paste0("<b>TRACT: ", ac_snap_map$NAMELSAD, "</b><br />PERCENT_SNAP: ", ac_snap_map$PERCENT_SNAP)
ac_snap_map <- st_transform(ac_snap_map, crs = '+proj=longlat +datum=WGS84')
leaflet(ac_snap_map) %>%
addTiles() %>%
addPolygons(stroke=FALSE,
smoothFactor = 0.2,
fillOpacity = .6,
popup=snap_popup, color=~snap_palette(ac_snap_map$PERCENT_SNAP))%>%
addLegend("topright", pal = snap_palette, values = ~PERCENT_SNAP,
title = "<b>Percent of Households Recieving Snap by Census Tract</b><br> ",
opacity = 1)