Load libraries
library(tidycensus)
library(tidyverse)
library(sf)
library(tmap)
Searching for the poverty variable in the ACS code book
Kindly note you have to have connected your R to the census api at
some point for this code to work. If you haven’t go to https://api.census.gov/data/key_signup.html .
# census_api_key(key = "YOUR API GOES HERE", install = T)
v19_Profile <- load_variables(2019,
"acs5/profile",
cache = TRUE) #demographic
Using the key word “BELOW THE POVERTY LEVEL” to narrow the
search
v19_Profile[grep(x = v19_Profile$label,
"BELOW THE POVERTY LEVEL",
ignore.case = TRUE),
c("name", "label")]
## # A tibble: 38 × 2
## name label
## <chr> <chr>
## 1 DP03_0119 Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE P…
## 2 DP03_0119P Percent!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE PA…
## 3 DP03_0120 Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE P…
## 4 DP03_0120P Percent!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE PA…
## 5 DP03_0121 Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE P…
## 6 DP03_0121P Percent!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE PA…
## 7 DP03_0122 Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE P…
## 8 DP03_0122P Percent!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE PA…
## 9 DP03_0123 Estimate!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE P…
## 10 DP03_0123P Percent!!PERCENTAGE OF FAMILIES AND PEOPLE WHOSE INCOME IN THE PA…
## # … with 28 more rows
The above search result shows that the variable code name for
percent of families and people below the poverty level is
DP03_0119P
Using tidycensus, get the 5-years ACS data for ZIP Code Tabulation
Areas(ZCTA). Data for 2020 is not available at the zcta level
pov19<-get_acs(geography = "zcta",
state="TX",
#county = "Bexar",
year = 2019,
variables="DP03_0119PE" ,
geometry = T,
output = "wide")
## Getting data from the 2015-2019 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using the ACS Data Profile
#Requested the data for the whole of Texas.
rename variables and filter missing cases
pov19 <- pov19%>%
mutate(ppov = DP03_0119PE,
ppov_er = DP03_0119PM/1.645,
ppov_cv =100* (ppov_er/ppov)) %>%
filter(complete.cases(ppov), is.finite(ppov_cv)==T)%>%
select(GEOID, ppov, ppov_er,ppov_cv)
head(pov19)
## Simple feature collection with 6 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -97.41441 ymin: 28.13976 xmax: -93.79455 ymax: 32.68616
## Geodetic CRS: NAD83
## GEOID ppov ppov_er ppov_cv geometry
## 1 75973 22.2 5.835866 26.28769 MULTIPOLYGON (((-94.08422 3...
## 2 77414 15.7 2.066869 13.16477 MULTIPOLYGON (((-96.07715 2...
## 3 78377 12.2 3.708207 30.39514 MULTIPOLYGON (((-97.41441 2...
## 4 77656 9.5 2.006079 21.11662 MULTIPOLYGON (((-94.18403 3...
## 5 77465 12.8 5.167173 40.36854 MULTIPOLYGON (((-96.45207 2...
## 6 75672 8.8 1.762918 20.03316 MULTIPOLYGON (((-94.45825 3...
FIRST SCENERIO- MAPPING FOR ALL ZIP CODES IN SAN ANTONIO
Data was provided for ZCTAs in whole of texas, but we only need for
san antonio, hence, filtering for only san antoino
pov19$GEOID<- as.numeric(pov19$GEOID) #need to convert to numeric data type for the purpose of filtering
pov19_2<- pov19 #creating a copy for later use
pov19_2 <- pov19_2 %>%
filter(GEOID >= 78201 & GEOID <= 78299) # Only san antonio
San Antonio Poverty Rate Estimates - Quantile Breaks
tmap_mode("plot") #This is for the map to be static
## tmap mode set to plotting
#tmap_mode("view") # This is fpr the map to be interactive
map1<- tm_shape(pov19_2) +
tm_polygons(c("ppov"),
title = c("% in Poverty"),
palette = "Blues",
style = "quantile",
n = 5) +
tm_text("GEOID",
size = 0.5) +
tm_scale_bar() +
tm_layout(title = "San Antonio Poverty Rate Estimates - Quantile Breaks",
title.size = 1.5,
legend.frame = TRUE,
title.position = c("right", "top")) +
tm_compass() +
tm_format("World",
legend.position = c("left", "bottom"),
main.title.position = c("center"))
tmap_save(map1, filename = "san_antonio_poverty_all.png", width = 8, height = 6, dpi = 300) # This will save the map in your working directory
## Map saved to /Users/josephjaiyeola/Library/CloudStorage/OneDrive-UniversityofTexasatSanAntonio/Income mobility Analysis/san_antonio_poverty_all.png
## Resolution: 2400 by 1800 pixels
## Size: 8 by 6 inches (300 dpi)
map1

SECOND SCENERIO- MAPPING FOR ONLY ZIP CODES IN SAN ANTONIO PROVIDED
IN EXCEL SHEET. However, zip code 76549,78109 and 78744 were excluded
because they are not in San Antonio
pov19_excel <- pov19[pov19$GEOID %in% c(78109, 78201, 78202, 78203, 78204, 78205, 78207, 78208, 78209, 78210, 78211, 78212, 78213, 78214, 78216, 78217, 78218, 78219, 78220, 78221, 78222, 78223, 78224, 78225, 78226, 78227, 78228, 78229, 78230, 78231, 78233, 78237, 78238, 78239, 78240, 78242, 78244, 78245, 78247, 78249, 78250, 78251, 78252, 78253, 78254, 78256, 78257, 78258, 78264), ]
# Check the filtered dataset
head(pov19_excel)
## Simple feature collection with 6 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -98.67006 ymin: 29.37843 xmax: -98.44113 ymax: 29.6642
## Geodetic CRS: NAD83
## GEOID ppov ppov_er ppov_cv geometry
## 45 78203 21.6 5.045593 23.359225 MULTIPOLYGON (((-98.47621 2...
## 70 78237 26.1 2.127660 8.151952 MULTIPOLYGON (((-98.59969 2...
## 166 78208 39.8 5.167173 12.982847 MULTIPOLYGON (((-98.47396 2...
## 168 78225 26.7 3.768997 14.116093 MULTIPOLYGON (((-98.54384 2...
## 192 78256 2.3 1.215805 52.861107 MULTIPOLYGON (((-98.66062 2...
## 563 78249 7.9 1.094225 13.850948 MULTIPOLYGON (((-98.67006 2...
San Antonio Poverty Rate Estimates(EXCEL SHEET ZIP CODES) - Quantile
Breaks
tmap_mode("plot") #This is for the map to be static
## tmap mode set to plotting
#tmap_mode("view") # This is fpr the map to be interactive
map2 <- tm_shape(pov19_excel) +
tm_polygons(c("ppov"),
title = c("% in Poverty"),
palette = "Blues",
style = "quantile",
n = 5) +
tm_text("GEOID",
size = 0.5) +
tm_scale_bar() +
tm_layout(title = "San Antonio Poverty Rate Estimates - Quantile Breaks",
title.size = 1.5,
legend.frame = TRUE,
title.position = c("right", "top")) +
tm_compass() +
tm_format("World",
legend.position = c("left", "bottom"),
main.title.position = c("center"))
tmap_save(map2, filename = "san_antonio_poverty_excel.png", width = 8, height = 6, dpi = 300) # This will save the map in your working directory
## Map saved to /Users/josephjaiyeola/Library/CloudStorage/OneDrive-UniversityofTexasatSanAntonio/Income mobility Analysis/san_antonio_poverty_excel.png
## Resolution: 2400 by 1800 pixels
## Size: 8 by 6 inches (300 dpi)
map2

ADDITIONAL- EXAMPLE OF IF YOU WANT THE MAP TO BE INTERACTIVE( SAME
AS ABOVE)
San Antonio Poverty Rate Estimates(EXCEL SHEET ZIP CODES) - Quantile
Breaks
#tmap_mode("plot") #This is for the map to be static
tmap_mode("view") # This is fpr the map to be interactive
## tmap mode set to interactive viewing
tm_shape(pov19_excel) +
tm_polygons(c("ppov"),
title = c("% in Poverty"),
palette = "Blues",
style = "quantile",
n = 5) +
tm_text("GEOID",
size = 0.5) +
tm_scale_bar() +
tm_layout(title = "San Antonio Poverty Rate Estimates - Quantile Breaks",
title.size = 1.5,
legend.frame = TRUE,
title.position = c("right", "top")) +
tm_compass() +
tm_format("World",
legend.position = c("left", "bottom"),
main.title.position = c("center"))
## Compass not supported in view mode.
## legend.postion is used for plot mode. Use view.legend.position in tm_view to set the legend position in view mode.