Lab 6 API

ZIP code map showing the ZIP code, estimated rent for each unit size, the estimated number of rental units and total households, and error margins for each. The map shows a color code for the average rent as the average goes higher or lower.

Code:

# Installing and loading required packages

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

library(tidyverse)
library(gtExtras)
library(sf)
library(mapview)
library(leafpop)
library(RColorBrewer)
library(tidycensus)

census_api_key("1cd3e7f0a7bab2686341294de73f4a2373a4f206")

Census_Data <- get_acs(
  geography = "zcta",
  variables = c("DP04_0047", "DP04_0045"),
  year = 2023,
  survey = "acs5",
  output = "wide",
  geometry = FALSE
)

glimpse(Census_Data)

Census_Data <- Census_Data %>% 
  rename(c("Rentals" = "DP04_0047E",
           "Rentals_MOE" = "DP04_0047M",
           "Households" = "DP04_0045E",
           "Households_MOE" = "DP04_0045M"))

glimpse(Census_Data)

# Redownloading the rent data

FMR_RuCo <- read_csv("https://raw.githubusercontent.com/drkblake/Data/refs/heads/main/FMR_RuCo.csv")

# Redownloading, unzipping and importing the ZIP code map file

download.file(
  "https://www2.census.gov/geo/tiger/GENZ2020/shp/cb_2020_us_zcta520_500k.zip",
  "ZCTAs2020.zip"
)

unzip("ZCTAs2020.zip")

ZCTAMap <- read_sf("cb_2020_us_zcta520_500k.shp")

# Merging the rent data and ZIP code map

FMR_RuCo$ZIP <- as.character(FMR_RuCo$ZIP)

FMR_RuCo_Map <- left_join(FMR_RuCo, ZCTAMap, by = c("ZIP" = "ZCTA5CE20"))

FMR_RuCo_Map <- FMR_RuCo_Map %>%
  select(-c(AFFGEOID20, GEOID20, NAME20, LSAD20, ALAND20, AWATER20))

FMR_RuCo_Map <- st_as_sf(FMR_RuCo_Map)

# Mapping by average rent with "YlOrRd"

ZIP_Map <- mapview(
  FMR_RuCo_Map,
  zcol = "ZIP_Average",
  col.regions = brewer.pal(9, "YlOrRd"),
  layer.name = "Average rent",
  popup = popupTable(
    FMR_RuCo_Map,
    feature.id = FALSE,
    row.numbers = FALSE,
    zcol = c("ZIP", "Studio", "BR1", "BR2", "BR3", "BR4")
  )
)

# Showing the map

ZIP_Map

# Merging FMR_RuCo_Map and Census_Data

FMR_RuCo_Map <- left_join(FMR_RuCo_Map, Census_Data, by = c("ZIP" = "GEOID"))

# Mapping by ZIP code

ZIP_Map <- mapview(
  FMR_RuCo_Map,
  zcol = "ZIP_Average",
  col.regions = brewer.pal(9, "YlOrRd"),
  layer.name = "Average rent",
  popup = popupTable(
    FMR_RuCo_Map,
    feature.id = FALSE,
    row.numbers = FALSE,
    zcol = c("ZIP", "Studio", "BR1", "BR2", "BR3", "BR4",
             "Rentals", "Rentals_MOE", "Households", "Households_MOE")
  )
)

# Showing the map

ZIP_Map

# ACS codebooks

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