#install.packages("tidycensus")
library(tidycensus)
<- load_variables(2021 , "acs5", cache = TRUE)
acs2021 library(dplyr)
<-get_acs(geography = "puma",
tx_pumastate="TX",
year = 2021,
survey = "acs5",
variables=c("B03002_001E",
"B03002_003E",
"B03002_004E",
"B03002_006E",
"B03002_007E",
"B03002_005E",
"B03002_008E",
"B03002_009E",
"B03002_012E"),
geometry = T, output = "wide")
#select tracts that have nonzero population
#dal_tract <- filter(dal_tract, B03002_001E>0)
#rename variables
<-tx_puma%>%
tx_pumamutate(totpop=B03002_001E,
whiteNH = B03002_003E,
blackNH = B03002_004E,
aapiNH = B03002_006E + B03002_007E,
aianNH = B03002_005E,
oth2NH = B03002_008E + B03002_009E,
HispLat = B03002_012E,
pwhiteNH = whiteNH/totpop,
pblackNH = blackNH/totpop,
paapiNH = aapiNH/ totpop,
paianNH = aianNH/totpop,
poth2NH = oth2NH/ totpop,
pHispLat = HispLat / totpop,
pctPOC = 1 - pwhiteNH)
<-get_acs(geography = "place", state="Texas", variables=c("DP05_0001E"), year=2021, geometry = T, output = "wide")
tx_places<-tx_places[tx_places$GEOID=="4819000",]
dallas_place<-c(4802304,4802305,4802306,4802307,4802309,4802310,4802311,4802312,4802313,4802314,4802315,4802316,4802319,4801901,4802001)
dal_codes<-c("4802304","4802305","4802306","4802307","4802309","4802310","4802311","4802312","4802313","4802314","4802315","4802316","4802319","4801901","4802001")
dal_codes_string<-tx_puma[tx_puma$GEOID %in% dal_codes,]
dallas_puma
#install.packages("tmap")
library(tmap)
tmap_mode("view")
library(sf)
<- st_bbox(dallas_place)
dallas_bb<-st_bbox(dallas_puma)
puma_bb
$nb_race <- ifelse(dallas_puma$pwhiteNH>0.5,"Majority White NH",(ifelse(dallas_puma$pblackNH>0.5,"Majority Black NH",(ifelse(dallas_puma$paapiNH>0.5,"Majority AAPI NH",(ifelse(dallas_puma$poth2NH>0.5,"Majority Other or 2+ NH",(ifelse(dallas_puma$pHispLat>0.5,"Majority Hispanic/Latino","Racially Diverse")))))))))
dallas_puma
<- dallas_puma[,c("GEOID", "NAME", "totpop", "whiteNH", "blackNH", "aapiNH", "aianNH", "oth2NH", "HispLat", "pwhiteNH", "pblackNH", "paapiNH", "paianNH", "poth2NH", "pHispLat", "pctPOC", "nb_race")] #selecting only the columns I want
dallas_puma_exp
st_write(dallas_puma_exp, "dal_puma_race_eth.shp", append=FALSE)
Dallas Geographies and Race/Eth Data Exports
Exporting Maps + Creating Neighborhood Designations
From p. 13 of 2019 Report: “Although most of the indicators that compare outcomes by race/ethnicity use individual-level data, this data was unavailable in some instances. For those indicators, neighborhoods (defined by census tracts or zip codes) are used as a proxy. In this report, we compare neighborhoods according to their majority (more than 50%) racial/ethnic makeup. If no majority exists, we label that neighborhood”racially diverse.” In Dallas, the following majority racial/ethnic groups for neighborhoods are used: White, Black, and Hispanic. No neighborhoods were majority-Asian in either year.”
Note: for tract and zip code data, we have to use 5 year ACS. This will be used for both the 2019 and 2021 samples.
Data Pulls + Calcs: PUMAs
Data Pulls + Calcs: Census Tracts
Note that there are many more geographies shown here than we use in the report - this was casting a wide net and just gathering all tracts for the five counties that intersect the City of Dallas.
#NOTE: 5 Year ACS for tracts in Dallas' intersecting Counties (Dallas, Collin, Denton, Kaufman, Rockwall)
<- df <- data.frame (CountyName = c("Dallas", "Collin", "Denton", "Kaufman", "Rockwall"),
dal_counties CountyFIPS3 = c("113","085","121","257","397"),
CountyFIPS5 = c("48113","48085","48121","48257","48397"))
<-get_acs(geography = "tract",
dal_tractstate="TX",
county = dal_counties$CountyFIPS3,
year = 2021,
survey = "acs5",
variables=c("B03002_001E",
"B03002_003E",
"B03002_004E",
"B03002_006E",
"B03002_007E",
"B03002_005E",
"B03002_008E",
"B03002_009E",
"B03002_012E"),
geometry = T, output = "wide")
#select tracts that have nonzero population
#dal_tract <- filter(dal_tract, B03002_001E>0)
#rename variables
<-dal_tract%>%
dal_tractmutate(totpop=B03002_001E,
whiteNH = B03002_003E,
blackNH = B03002_004E,
aapiNH = B03002_006E + B03002_007E,
aianNH = B03002_005E,
oth2NH = B03002_008E + B03002_009E,
HispLat = B03002_012E,
pwhiteNH = whiteNH/totpop,
pblackNH = blackNH/totpop,
paapiNH = aapiNH/ totpop,
paianNH = aianNH/totpop,
poth2NH = oth2NH/ totpop,
pHispLat = HispLat / totpop,
pctPOC = 1 - pwhiteNH)
$nonzero_pop <- ifelse(dal_tract$totpop>0,1,0)
dal_tract
$nb_race <- ifelse(dal_tract$pwhiteNH>0.5,"Majority White NH",(ifelse(dal_tract$pblackNH>0.5,"Majority Black NH",(ifelse(dal_tract$paapiNH>0.5,"Majority AAPI NH",(ifelse(dal_tract$poth2NH>0.5,"Majority Other or 2+ NH",(ifelse(dal_tract$pHispLat>0.5,"Majority Hispanic/Latino",(ifelse(dal_tract$nonzero_pop==1,"Racially Diverse",0)))))))))))
dal_tract
table(dal_tract$neighb_race)
<- dal_tract[,c("GEOID", "NAME", "totpop", "whiteNH", "blackNH", "aapiNH", "aianNH", "oth2NH", "HispLat", "pwhiteNH", "pblackNH", "paapiNH", "paianNH", "poth2NH", "pHispLat", "pctPOC", "nb_race")] #selecting only the columns I want
dal_tract_exp
st_write(dal_tract_exp, "dal_tract_race_eth.shp", append=FALSE)
#install.packages("writexl")
#library("writexl")
#write_xlsx(dal_tract,"~/Desktop/Temp/table.xlsx")
#grab dallas city boundary
<-get_acs(geography = "place", state="Texas", variables=c("DP05_0001E"), year=2021, geometry = T, output = "wide")
tx_places<-tx_places[tx_places$GEOID=="4819000",]
dallas_placest_write(dallas_place, "city_dallas_place.shp", append=FALSE)
Data Pulls + Calcs: Zip Codes
<-get_acs(geography = "county",
dal_counties_geoyear = 2021,
state = "TX",
county = dal_counties$CountyFIPS3,
survey = "acs5",
variables=c("B03002_001E"),
geometry = T, output = "wide")
<-get_acs(geography = "zip code tabulation area",
us_zipyear = 2021,
survey = "acs5",
variables=c("B03002_001E",
"B03002_003E",
"B03002_004E",
"B03002_006E",
"B03002_007E",
"B03002_005E",
"B03002_008E",
"B03002_009E",
"B03002_012E"),
geometry = T, output = "wide")
#filter for dallas Zip codes (intersect the counties)
<- us_zip[dal_counties_geo, , op = st_intersects]
dal_zip
#rename variables
<-dal_zip%>%
dal_zipmutate(totpop=B03002_001E,
whiteNH = B03002_003E,
blackNH = B03002_004E,
aapiNH = B03002_006E + B03002_007E,
aianNH = B03002_005E,
oth2NH = B03002_008E + B03002_009E,
HispLat = B03002_012E,
pwhiteNH = whiteNH/totpop,
pblackNH = blackNH/totpop,
paapiNH = aapiNH/ totpop,
paianNH = aianNH/totpop,
poth2NH = oth2NH/ totpop,
pHispLat = HispLat / totpop,
pctPOC = 1 - pwhiteNH)
$nonzero_pop <- ifelse(dal_zip$totpop>0,1,0)
dal_zip
$nb_race <- ifelse(dal_zip$pwhiteNH>0.5,"Majority White NH",(ifelse(dal_zip$pblackNH>0.5,"Majority Black NH",(ifelse(dal_zip$paapiNH>0.5,"Majority AAPI NH",(ifelse(dal_zip$poth2NH>0.5,"Majority Other or 2+ NH",(ifelse(dal_zip$pHispLat>0.5,"Majority Hispanic/Latino",(ifelse(dal_zip$nonzero_pop==1,"Racially Diverse",0)))))))))))
dal_zip
table(dal_zip$neighb_race)
<- dal_zip[,c("GEOID", "NAME", "totpop", "whiteNH", "blackNH", "aapiNH", "aianNH", "oth2NH", "HispLat", "pwhiteNH", "pblackNH", "paapiNH", "paianNH", "poth2NH", "pHispLat", "pctPOC", "nb_race")] #selecting only the columns I want
dal_zip_exp
#export to shapefile
st_write(dal_zip_exp, "dal_zip_race_eth.shp", append=FALSE)
Maps for Three Geographies
PUMA Map 1: Percent POC Population by Tract (2021 5 YR ACS)
library(tmap)
tm_shape(dallas_puma,bbox = dallas_bb)+
tm_polygons("pctPOC",
title="PUMAs",
border.alpha = 0.25,
palette="YlGnBu",
border.col = "black",
style="pretty",
legend.hist=TRUE)+
tm_format("World",
title="PUMA Share Population of Color, 2017-2021 5 YR ACS",
legend.outside=T)+
tm_shape(dallas_place)+
tm_polygons(alpha=0,border.col="black")+
tm_scale_bar(position="LEFT")
PUMA Map 2: Neighborhood Racial Categorization
tm_shape(dallas_puma,bbox = dallas_bb)+
tm_polygons("nb_race",
title="PUMAs",
border.alpha = 0.25,
palette="Set2",
border.col = "black",
style="pretty",
legend.hist=TRUE)+
tm_format("World",
title="PUMA Categorization, 2017-2021 5 YR ACS",
legend.outside=T)+
tm_shape(dallas_place)+
tm_polygons(alpha=0,border.col="black")+
tm_scale_bar(position="LEFT")
#Below to explore color palettes via Color Brewer
# library (tmaptools)
# install.packages("shinyjs")
# palette_explorer()
# get_brewer_pal("Set2", n = 5)
Tract Map 1: Percent POC Population by Tract (2021 5 YR ACS)
tm_shape(dal_tract,bbox = dallas_bb)+
tm_polygons("pctPOC",
title="Tracts",
border.alpha = 0.25,
palette="YlGnBu",
border.col = "black",
style="pretty",
legend.hist=TRUE)+
tm_format("World",
title="Tract Categorization, 2017-2021 5 YR ACS",
legend.outside=T)+
tm_shape(dallas_place)+
tm_polygons(alpha=0,border.col="black")+
tm_scale_bar(position="LEFT")
Tract Map 2: Neighborhood Racial Categorization
tm_shape(dal_tract,bbox = dallas_bb)+
tm_polygons("nb_race",
title="Tracts",
border.alpha = 0.25,
palette="Set2",
border.col = "black",
style="pretty",
legend.hist=TRUE)+
tm_format("World",
title="Tract Categorization, 2017-2021 5 YR ACS",
legend.outside=T)+
tm_shape(dallas_place)+
tm_polygons(alpha=0,border.col="black")+
tm_scale_bar(position="LEFT")
ZCTA Map 1: Percent POC Population by Zip Code (2021 5 YR ACS)
tm_shape(dal_zip,bbox = dallas_bb)+
tm_polygons("pctPOC",
title="Zip Code",
border.alpha = 0.25,
palette="YlGnBu",
border.col = "black",
style="pretty",
legend.hist=TRUE)+
tm_format("World",
title="Zip Code Share Population of Color, 2017-2021 5 YR ACS",
legend.outside=T)+
tm_shape(dallas_place)+
tm_polygons(alpha=0,border.col="black")+
tm_scale_bar(position="LEFT")
ZCTA Map 2: Neighborhood Racial Categorization
tm_shape(dal_zip,bbox = dallas_bb)+ tm_polygons("nb_race",title="Zip Codes",border.alpha = 0.25, palette="Set2", border.col = "black",style="pretty",legend.hist=TRUE)+ tm_format("World", title="Zip Code Categorization, 2017-2021 5 YR ACS", legend.outside=T)+ tm_shape(dallas_place)+ tm_polygons(alpha=0,border.col="black")+ tm_scale_bar(position="LEFT")+ tm_compass()
Compass not supported in view mode.