Three Bedroom Rent Map

Three Bedroom Rent Map

Three-Bedroom Map

Code:

# Ensure necessary packages are installed and loaded
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")

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

# Read the CSV file first (FMR_RuCo dataset)
FMR_RuCo <- read_csv("https://raw.githubusercontent.com/drkblake/Data/refs/heads/main/FMR_RuCo.csv")

# Now, proceed with the rest of the code

# Showing the rent data
FMR_RuCo_table <- gt(FMR_RuCo) %>%
  tab_header("Rutherford FMR, by size and ZIP") %>%
  cols_align(align = "left") %>%
  gt_theme_538
FMR_RuCo_table

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

# Unzip shapefile
unzip("ZCTAs2020.zip")

# Read shapefile (ZIP code boundaries)
ZCTAMap <- read_sf("cb_2020_us_zcta520_500k.shp")

# Convert ZIP to character
FMR_RuCo$ZIP <- as.character(FMR_RuCo$ZIP)

# Join the rental data with the shapefile
FMR_RuCo_Map <- left_join(FMR_RuCo, ZCTAMap, by = c("ZIP" = "ZCTA5CE20"))

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

# Convert to spatial data frame
FMR_RuCo_Map <- st_as_sf(FMR_RuCo_Map)

# Make the map for rent categories
Rent_Category_Map <- mapview(
  FMR_RuCo_Map,
  zcol = "Rent_Category",
  layer.name = "Rent category",
  popup = popupTable(
    FMR_RuCo_Map,
    feature.id = FALSE,
    row.numbers = FALSE,
    zcol = c("ZIP", "Studio", "BR1", "BR2", "BR3", "BR4"))
)

# Show the map
Rent_Category_Map

# Make the map for three-bedroom rent (BR3)
Rent_Three_Bedroom_Map <- mapview(
  FMR_RuCo_Map,
  zcol = "BR3",  # Use BR3 column for three-bedroom rent
  layer.name = "Three Bedroom Rent",
  popup = popupTable(
    FMR_RuCo_Map,
    feature.id = FALSE,
    row.numbers = FALSE,
    zcol = c("ZIP", "Studio", "BR1", "BR2", "BR3", "BR4"))
)

# Show the map with three-bedroom rent data
Rent_Three_Bedroom_Map

# Create a custom color palette for BR3 (three-bedroom rent)
pal <- colorNumeric(palette = "YlOrRd", domain = FMR_RuCo_Map$BR3)

# Map with custom color palette for three-bedroom rent
Rent_Three_Bedroom_Map_Custom <- mapview(
  FMR_RuCo_Map,
  zcol = "BR3",  # Three-bedroom rent
  layer.name = "Three Bedroom Rent",
  color = pal(FMR_RuCo_Map$BR3),
  popup = popupTable(
    FMR_RuCo_Map,
    feature.id = FALSE,
    row.numbers = FALSE,
    zcol = c("ZIP", "Studio", "BR1", "BR2", "BR3", "BR4"))
)

# Show the custom map
Rent_Three_Bedroom_Map_Custom

# Create a pink custom color palette for BR3 (three-bedroom rent)
pal <- colorNumeric(
  palette = c("pink", "darkred"),
  domain = FMR_RuCo_Map$BR3
)

# Map with pink custom color palette for three-bedroom rent
Rent_Three_Bedroom_Map_Pink <- mapview(
  FMR_RuCo_Map,
  zcol = "BR3",  # Three-bedroom rent
  layer.name = "Three Bedroom Rent",
  color = pal(FMR_RuCo_Map$BR3),
  popup = popupTable(
    FMR_RuCo_Map,
    feature.id = FALSE,
    row.numbers = FALSE,
    zcol = c("ZIP", "Studio", "BR1", "BR2", "BR3", "BR4"))
)

# Show the pink custom color map
Rent_Three_Bedroom_Map_Pink
# Manually unzip the shapefile if downloaded
unzip("ZCTAs2020.zip")

# Read the shapefile
ZCTAMap <- read_sf("cb_2020_us_zcta520_500k.shp")