The NDI Package was created by Ian D. Buller. It includes several depravation indices. Here we will use the NDI Deprivation Index.
First, load packages
#|warning: falselibrary(waywiser)library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(tidyr)library(sfdep)library(spdep)
Loading required package: sp
Loading required package: spData
To access larger datasets in this package, install the spDataLarge
package with: `install.packages('spDataLarge',
repos='https://nowosad.github.io/drat/', type='source')`
Loading required package: sf
Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(gridExtra)
Attaching package: 'gridExtra'
The following object is masked from 'package:dplyr':
combine
library(grid)library(ndi)
Welcome to {ndi} version 0.1.2
> help("ndi") # for documentation
> citation("ndi") # for how to cite
library(ggplot2)library(sf)library(tidycensus) # a dependency for the "ndi"" packagelibrary(tigris) # a dependency for the "ndi"" package
To enable caching of data, set `options(tigris_use_cache = TRUE)`
in your R script or .Rprofile.
options(scipen=999)
Second, call the ndi values and load geometries
# Compute the NDI (Messer) values (2016-2020 5-year ACS) for TX census tractscensus_api_key("42539e850e81857ea4d3a8219088cab9544e88bb")TX2020messer <- ndi::messer(state ="TX", year =2020)# Obtain the 2020 census tracts from the "tigris" packagetract2020TX <- tigris::tracts(state ="TX", year =2020, cb =TRUE)# Join the NDI (Messer) values to the census tract geometryTX2020messer <-merge(tract2020TX, TX2020messer$ndi, by ="GEOID")TX2020messer
Third, visualize the index
# Visualize the NDI (Messer) values for TX, U.S.A., counties## Continuous Indexggplot2::ggplot() + ggplot2::geom_sf(data = TX2020messer, ggplot2::aes(fill = NDI),size =0.20,color ="white") + ggplot2::theme_minimal() + ggplot2::scale_fill_viridis_c() + ggplot2::labs(fill ="Index (Continuous)",caption ="Source: U.S. Census ACS 2020 5 Year estimates") + ggplot2::ggtitle("Neighborhood Deprivation Index (Messer)",subtitle ="TX counties as the referent")
Alternative option: Visualize with quartiles
## Categorical Index### Rename "9-NDI not avail" level as NA for plottingTX2020messer$NDIQuartNA <-factor(replace(as.character(TX2020messer$NDIQuart), TX2020messer$NDIQuart =="9-NDI not avail", NA),c(levels(TX2020messer$NDIQuart)[-5], NA))plot2<-ggplot2::ggplot() + ggplot2::geom_sf(data = TX2020messer, ggplot2::aes(fill = NDIQuartNA),size =0.20,color ="white") + ggplot2::theme_minimal() + ggplot2::scale_fill_viridis_d(guide = ggplot2::guide_legend(reverse =TRUE),na.value ="grey80") + ggplot2::labs(fill ="Index (Categorical)",caption ="Source: U.S. Census ACS 2020 5-Year estimates") + ggplot2::ggtitle("Neighborhood Deprivation Index (Messer) Quartiles",subtitle ="TX counties as the referent") plot2