# Compute the NDI (Messer) values (2016-2020 5-year ACS) for TX census tracts
TX2020messer <- ndi::messer(state = "TX", year = 2020)
## Warning: Missing census data
# Obtain the 2020 census tracts from the "tigris" package
tract2020TX <- tigris::tracts(state = "TX", year = 2020, cb = TRUE)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |=======                                                               |  11%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |================================                                      |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |===================================                                   |  51%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |============================================================          |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
# Join the NDI (Messer) values to the census tract geometry
TX2020messer <- merge(tract2020TX, TX2020messer$ndi, by = "GEOID")

TX2020messer
## Simple feature collection with 6884 features and 26 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -106.6456 ymin: 25.83738 xmax: -93.50829 ymax: 36.5007
## Geodetic CRS:  NAD83
## First 10 features:
##          GEOID STATEFP COUNTYFP TRACTCE             AFFGEOID    NAME
## 1  48001950100      48      001  950100 1400000US48001950100    9501
## 2  48001950401      48      001  950401 1400000US48001950401 9504.01
## 3  48001950402      48      001  950402 1400000US48001950402 9504.02
## 4  48001950500      48      001  950500 1400000US48001950500    9505
## 5  48001950600      48      001  950600 1400000US48001950600    9506
## 6  48001950700      48      001  950700 1400000US48001950700    9507
## 7  48001950800      48      001  950800 1400000US48001950800    9508
## 8  48001950901      48      001  950901 1400000US48001950901 9509.01
## 9  48001950902      48      001  950902 1400000US48001950902 9509.02
## 10 48001951001      48      001  951001 1400000US48001951001 9510.01
##                NAMELSAD STUSPS      NAMELSADCO STATE_NAME LSAD     ALAND
## 1     Census Tract 9501     TX Anderson County      Texas   CT 483306613
## 2  Census Tract 9504.01     TX Anderson County      Texas   CT  16509268
## 3  Census Tract 9504.02     TX Anderson County      Texas   CT  71134275
## 4     Census Tract 9505     TX Anderson County      Texas   CT  23132052
## 5     Census Tract 9506     TX Anderson County      Texas   CT  20653882
## 6     Census Tract 9507     TX Anderson County      Texas   CT   6720934
## 7     Census Tract 9508     TX Anderson County      Texas   CT  10429389
## 8  Census Tract 9509.01     TX Anderson County      Texas   CT 290214322
## 9  Census Tract 9509.02     TX Anderson County      Texas   CT 441347126
## 10 Census Tract 9510.01     TX Anderson County      Texas   CT 358736667
##     AWATER state          county   tract     NDI               NDIQuart OCC CWD
## 1  7864313 Texas Anderson County    9501 -0.0021 3-AboveAvg deprivation 0.1 0.0
## 2   298419 Texas Anderson County 9504.01 -0.0251 2-BelowAvg deprivation 0.0 0.1
## 3  2626492 Texas Anderson County 9504.02     NaN        9-NDI not avail NaN NaN
## 4    99223 Texas Anderson County    9505  0.0074 3-AboveAvg deprivation 0.0 0.1
## 5   329641 Texas Anderson County    9506  0.0203 3-AboveAvg deprivation 0.0 0.0
## 6     6724 Texas Anderson County    9507  0.0347 3-AboveAvg deprivation 0.0 0.1
## 7    92101 Texas Anderson County    9508  0.0145 3-AboveAvg deprivation 0.0 0.1
## 8  4738880 Texas Anderson County 9509.01 -0.0195 2-BelowAvg deprivation 0.1 0.0
## 9  4984901 Texas Anderson County 9509.02  0.0043 3-AboveAvg deprivation 0.0 0.0
## 10 3015204 Texas Anderson County 9510.01  0.0127 3-AboveAvg deprivation 0.0 0.0
##    POV FHH PUB U30 EDU EMP                       geometry
## 1  0.2 0.1 0.1 0.3 0.1 0.1 MULTIPOLYGON (((-95.69483 3...
## 2  0.0 0.1 0.1 0.0 0.3 0.0 MULTIPOLYGON (((-95.84786 3...
## 3  NaN NaN NaN NaN 0.3 NaN MULTIPOLYGON (((-95.98345 3...
## 4  0.1 0.1 0.1 0.2 0.1 0.0 MULTIPOLYGON (((-95.68779 3...
## 5  0.2 0.1 0.2 0.4 0.3 0.0 MULTIPOLYGON (((-95.70758 3...
## 6  0.2 0.1 0.1 0.5 0.2 0.0 MULTIPOLYGON (((-95.64951 3...
## 7  0.1 0.1 0.2 0.3 0.0 0.0 MULTIPOLYGON (((-95.62881 3...
## 8  0.1 0.0 0.1 0.3 0.2 0.0 MULTIPOLYGON (((-95.88093 3...
## 9  0.1 0.0 0.2 0.3 0.2 0.0 MULTIPOLYGON (((-95.70501 3...
## 10 0.2 0.1 0.2 0.3 0.1 0.1 MULTIPOLYGON (((-95.58587 3...
# Visualize the NDI (Messer) values  for TX, U.S.A., counties
## Continuous Index
ggplot2::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")

## Categorical Index

### Rename "9-NDI not avail" level as NA for plotting
TX2020messer$NDIQuartNA <- factor(replace(as.character(TX2020messer$NDIQuart), 
                                            TX2020messer$NDIQuart == "9-NDI not avail", NA),
                                         c(levels(TX2020messer$NDIQuart)[-5], NA))

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")

# txmesser10 <- ndi::messer(state = "TX", year = 2010)
# 
# txmesser10$ndi

```