Skip to contents
library(sdc.census10to20)
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(readr)
library(tigris)
#> To enable caching of data, set `options(tigris_use_cache = TRUE)`
#> in your R script or .Rprofile.
library(ggplot2)
library(sf)
#> Linking to GEOS 3.10.2, GDAL 3.4.1, PROJ 8.2.1; sf_use_s2() is TRUE

load data

data(va_hdcttr_vdh_2017_2021_material_deprivation_index,
  package = "sdc.census10to20"
)
data <- df

filter to years and region_type that need to be redistributed

data <- data %>% filter(year <= 2019)
data <- data %>% filter(region_type == "tract")

use the standardize function

this function produces both standardized and original values (see the measure variable)

standardized_data <- standardize_all(data)
#> Warning in convert_2010_to_2020_bounds(temp): data contains missing or NULL values. the value of any new tract that
#>       overlaps with a NULL value will be coerced to NULL.
#>       If this is an issue, we recommend manual insertion of values based on
#>       your contextual specifications.
#> Warning in convert_2010_to_2020_bounds(temp): data contains missing or NULL values. the value of any new tract that
#>       overlaps with a NULL value will be coerced to NULL.
#>       If this is an issue, we recommend manual insertion of values based on
#>       your contextual specifications.
#> Warning in convert_2010_to_2020_bounds(temp): data contains missing or NULL values. the value of any new tract that
#>       overlaps with a NULL value will be coerced to NULL.
#>       If this is an issue, we recommend manual insertion of values based on
#>       your contextual specifications.

producing two maps with standardized and original index values for the year 2019

standardized_data <- standardized_data %>% filter(year == 2019)
standardized_data_std <- standardized_data %>%
  filter(measure == "material_deprivation_indicator_geo20")
standardized_data_org <- standardized_data %>%
  filter(measure == 'material_deprivation_indicator_geo10')

getting tract shape files for VA

virginia_tracts_2010 <- tracts(state = "51", year = 2010, cb = TRUE)
#>   |                                                                              |                                                                      |   0%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=======                                                               |  10%  |                                                                              |==========                                                            |  14%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |==============                                                        |  20%  |                                                                              |================                                                      |  22%  |                                                                              |=================                                                     |  24%  |                                                                              |==================                                                    |  26%  |                                                                              |===================                                                   |  28%  |                                                                              |=====================                                                 |  30%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  34%  |                                                                              |==========================                                            |  37%  |                                                                              |============================                                          |  39%  |                                                                              |=============================                                         |  42%  |                                                                              |===============================                                       |  45%  |                                                                              |==================================                                    |  49%  |                                                                              |====================================                                  |  51%  |                                                                              |=====================================                                 |  52%  |                                                                              |=======================================                               |  55%  |                                                                              |========================================                              |  58%  |                                                                              |==========================================                            |  60%  |                                                                              |===========================================                           |  62%  |                                                                              |=============================================                         |  64%  |                                                                              |===============================================                       |  67%  |                                                                              |================================================                      |  68%  |                                                                              |==================================================                    |  71%  |                                                                              |====================================================                  |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |=======================================================               |  79%  |                                                                              |=========================================================             |  81%  |                                                                              |===========================================================           |  84%  |                                                                              |============================================================          |  86%  |                                                                              |=============================================================         |  87%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  91%  |                                                                              |==================================================================    |  95%  |                                                                              |===================================================================   |  96%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================| 100%
virginia_tracts_2020 <- tracts(state = "51", year = 2020, cb = TRUE)
#>   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   2%  |                                                                              |===                                                                   |   4%  |                                                                              |====                                                                  |   5%  |                                                                              |=====                                                                 |   7%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |=========                                                             |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  22%  |                                                                              |=================                                                     |  24%  |                                                                              |==============================================                        |  65%  |                                                                              |=================================================                     |  70%  |                                                                              |===================================================                   |  73%  |                                                                              |=====================================================                 |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |=======================================================               |  79%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  85%  |                                                                              |============================================================          |  86%  |                                                                              |===============================================================       |  90%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  95%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  97%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================| 100%
virginia_tracts_2010$geoid <- substring(virginia_tracts_2010$GEO_ID, 10)
virginia_tracts_2020$geoid <- virginia_tracts_2020$GEOID

merging the standardized and original files with shapefiles


standardized_data_std <- merge(
  virginia_tracts_2020,
  standardized_data_std,
  by = "geoid",
  all.x = TRUE
)

standardized_data_org <- merge(
  virginia_tracts_2010,
  standardized_data_org,
  by = "geoid",
  all = TRUE
)

making standaardized map

my_plot_std <- ggplot(data = standardized_data_std) +
  geom_sf(aes(fill = value)) +
  scale_fill_gradient2(
    low = "white",
    high = "red",
    midpoint = median(standardized_data_std$value, na.rm = TRUE),
    space = "Lab",
    na.value = "grey50",
    limits = c(0, 0.5)
  ) +
  labs(title = 'Townsend Index - Standardized', fill = "Value") +
  theme_minimal()
# ggsave(
#   "standardized.png",
#   plot = my_plot,
#   width = 10,
#   height = 8,
#   dpi = 300
# )

making original map

my_plot_org <- ggplot(data = standardized_data_org) +
  geom_sf(aes(fill = value)) +
  scale_fill_gradient2(
    low = "white",
    high = "red",
    midpoint = median(standardized_data_org$value, na.rm = TRUE),
    space = "Lab",
    na.value = "grey50",
    limits = c(0, 0.5)
  ) +
  labs(title = 'Townsend Index - Original', fill = "Value") +
  theme_minimal()
# ggsave(
#   "original.png",
#   plot = my_plot,
#   width = 10,
#   height = 8,
#   dpi = 300
# )
par(mfrow=c(2,1))
plot(my_plot_std)

plot(my_plot_org)