Gross Rent as a Percentage of Income, or GRAPI, is a standard measure of housing affordability. This script will map Rutherford County’s subdivisions, then shade them according to the percentage of renters in each division who are paying 35 percent of more of their total household income on rent and utilities. The estimates come from the 2022 five-year American Community Survey.

Note that, to use the script, you must obtain a Census API key and paste it into the code in place of PasteYourAPIKeyBetweenTheseQuoteMarks, and delete the # character at the start of that line of code.

if (!require("tidyverse")) install.packages("tidyverse")
if (!require("tidycensus")) install.packages("tidycensus")
if (!require("mapview")) install.packages("mapview")
if (!require("sf")) install.packages("sf")

library(tidyverse)
library(tidycensus)
library(mapview)
library(sf)

census_api_key("PasteYourAPIKeyBetweenTheseQuoteMarks")


DetailedTables <- load_variables(2022, "acs5", cache = TRUE)
SubjectTables <- load_variables(2022, "acs5/subject", cache = TRUE)
ProfileTables <- load_variables(2022, "acs5/profile", cache = TRUE)

mydata <- get_acs(
  geography = "county subdivision",
  state = "TN",
  variables = c(MyVar_ = "DP04_0142P"),
  year = 2022,
  survey = "acs5",
  output = "wide",
  geometry = TRUE)

mydata <-
  separate_wider_delim(mydata,
                       NAME,
                       delim = ", ",
                       names = c("District", "County", "State"))

mydata <- filter(
  mydata,
  County == "Rutherford County")

mydata <- arrange(mydata, County, desc(MyVar_E))

write.csv(mydata, "mydata.csv", row.names = FALSE)

mydata_sf <- st_as_sf(mydata)

mydata_sf %>%
  ggplot(aes(fill = MyVar_E)) + 
  geom_sf(color = NA) + 
  scale_fill_viridis_c(option = "magma") 

mydata_mapview <- mapview(mydata_sf, zcol = "MyVar_E", 
                      col.regions = RColorBrewer::brewer.pal(9, "Blues"), alpha.regions = .5,
                      layer.name = "Pct. spending >34% on rent",#Customizable
                      popup = TRUE)

mydata_mapview

Here’s the script in action, with the output it produces:

if (!require("tidyverse")) install.packages("tidyverse")
if (!require("tidycensus")) install.packages("tidycensus")
if (!require("mapview")) install.packages("mapview")
if (!require("sf")) install.packages("sf")

library(tidyverse)
library(tidycensus)
library(mapview)
library(sf)

#census_api_key("PasteYourAPIKeyBetweenTheseQuoteMarks")


DetailedTables <- load_variables(2022, "acs5", cache = TRUE)
SubjectTables <- load_variables(2022, "acs5/subject", cache = TRUE)
ProfileTables <- load_variables(2022, "acs5/profile", cache = TRUE)

mydata <- get_acs(
  geography = "county subdivision",
  state = "TN",
  variables = c(MyVar_ = "DP04_0142P"),
  year = 2022,
  survey = "acs5",
  output = "wide",
  geometry = TRUE)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |===================                                                   |  28%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |==========================                                            |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  81%
  |                                                                            
  |=============================================================         |  88%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |====================================================================  |  98%
  |                                                                            
  |======================================================================| 100%
mydata <-
  separate_wider_delim(mydata,
                       NAME,
                       delim = ", ",
                       names = c("District", "County", "State"))

mydata <- filter(
  mydata,
  County == "Rutherford County")

mydata <- arrange(mydata, County, desc(MyVar_E))

write.csv(mydata, "mydata.csv", row.names = FALSE)

mydata_sf <- st_as_sf(mydata)

mydata_sf %>%
  ggplot(aes(fill = MyVar_E)) + 
  geom_sf(color = NA) + 
  scale_fill_viridis_c(option = "magma") 

mydata_mapview <- mapview(mydata_sf, zcol = "MyVar_E", 
                      col.regions = RColorBrewer::brewer.pal(9, "Blues"), alpha.regions = .5,
                      layer.name = "Pct. spending >34% on rent",#Customizable
                      popup = TRUE)

mydata_mapview
Pct. spending >34% on rent
20253035404550

10 km
5 mi
Leaflet | © OpenStreetMap contributors © CARTO