The script:
# Installing required packages
if (!require("dplyr")) install.packages("dplyr")
if (!require("tidyverse")) install.packages("tidyverse")
if (!require("mapview")) install.packages("mapview")
if (!require("leaflet")) install.packages("leaflet")
if (!require("leaflet.extras2")) install.packages("leaflet.extras2")
if (!require("tidycensus")) install.packages("tidycensus")
if (!require("sf")) install.packages("sf")
if (!require("openxlsx")) install.packages("openxlsx")
if (!require("scales")) install.packages("scales")
if (!require("leaflet.extras2")) install.packages("leaflet.extras2")
library(dplyr)
library(tidyverse)
library(ggplot2) #From the tidyverse package
library(readr) #From the tidyverse package
library(leaflet)
library(tidycensus)
library(sf)
library(mapview)
library(openxlsx)
library(leaflet.extras2)
library(leafpop)
library(scales)
options(tigris_use_cache = TRUE)
options(scipen = 999)
# Getting the map data
ZIPList <- c("37016",
"37026",
"37095",
"37118",
"37149",
"37190",
"37037",
"37060",
"37085",
"37086",
"37089",
"37118",
"37127",
"37128",
"37129",
"37130",
"37131",
"37132",
"37133",
"37149",
"37153",
"37167")
ZCTAMap <- get_acs(geography = "zcta",
variables = c(Under5_ = "B06001_002"),
year = 2021,
survey = "acs5",
output = "wide",
geometry = TRUE)
CountyMap <- get_acs(
geography = "county",
state = "TN",
variables = c(Under5_ = "B06001_002"),
year = 2021,
survey = "acs5",
output = "wide",
geometry = TRUE
)
CountyMap <- CountyMap %>%
filter(NAME == "Rutherford County, Tennessee"|
NAME == "Cannon County, Tennessee")
# Filtering and formatting the map data
RCMap <- ZCTAMap[ZCTAMap$GEOID %in% ZIPList,]
RCMap <- rename(RCMap, ZIP = GEOID)
ZIPMap <- mapview(CountyMap, zcol = "NAME",
legend = FALSE)+
mapview(RCMap, zcol = "ZIP",
legend=FALSE,
col.regions = RColorBrewer::brewer.pal(9, "Greys"), alpha.regions = .5,
layer.name = "ZIP Codes",
popup = TRUE)
ZIPMap