Code:
# ============================================================
# 0. INSTALL AND LOAD REQUIRED PACKAGES
# ============================================================
if (!require("tidyverse"))
install.packages("tidyverse")
if (!require("tidycensus"))
install.packages("tidycensus")
if (!require("sf"))
install.packages("sf")
if (!require("mapview"))
install.packages("mapview")
if (!require("leaflet"))
install.packages("leaflet")
if (!require("leaflet.extras2"))
install.packages("leaflet.extras2")
if (!require("gt"))
install.packages("gt")
if (!require("gtExtras"))
install.packages("gtExtras")
if (!require("plotly"))
install.packages("plotly")
library(tidyverse)
library(tidycensus)
library(sf)
library(mapview)
library(leaflet)
library(leafpop)
library(gt)
library(gtExtras)
library(plotly)
# ============================================================
# 1. CENSUS API KEY.
# NOTE: Replace PasteYourAPIKeyBetweenTheseQuoteMarks with your
# actual API key. If you don't the script won't work.
# ============================================================
census_api_key("f0e62cb9d2363c8c30463f42b91cfbabad1f4fb3")
# ============================================================
# 2. LOAD ACS CODEBOOKS
# ============================================================
DetailedTables <- load_variables(2023, "acs5", cache = TRUE)
SubjectTables <- load_variables(2023, "acs5/subject", cache = TRUE)
ProfileTables <- load_variables(2023, "acs5/profile", cache = TRUE)
# ============================================================
# 3. DEFINE VARIABLES OF INTEREST
# ============================================================
VariableList =
c(
Count_ = "DP02_0059",
Percent_ = "DP02_0068P"
)
# ============================================================
# 4. FETCH COUNTY SUBDIVISION DATA (TENNESSEE)
# ============================================================
mydata <- get_acs(
geography = "county subdivision",
state = "TN",
variables = VariableList,
year = 2023,
survey = "acs5",
output = "wide",
geometry = TRUE
)
# ============================================================
# 5. CLEAN AND REFORMAT GEOGRAPHIC NAMES
# ============================================================
mydata <- mydata %>%
separate_wider_delim(
NAME,
delim = ", ",
names = c("Division", "County", "State")
) %>%
mutate(County = str_remove(County, " County"))
# ============================================================
# 6. FILTER FOR A SINGLE COUNTY
# ============================================================
filtereddata <- mydata %>%
filter(County %in% c("Rutherford"))
# ============================================================
# 7. MAP DATA FOR SINGLE COUNTY SUBDIVISIONS
# ============================================================
mapdata <- filtereddata %>%
rename(
Percent = Percent_E,
PercentEM = Percent_M,
Count = Count_E,
CountEM = Count_M
) %>%
st_as_sf()
mapviewOptions(basemaps.color.shuffle = FALSE)
DivisionMap <- mapview(
mapdata,
zcol = "Percent",
layer.name = "Percent",
popup = popupTable(
mapdata,
feature.id = FALSE,
row.numbers = FALSE,
zcol = c("State", "County", "Division", "Percent",
"PercentEM", "Count", "CountEM")
)
)
DivisionMap
# ============================================================
# 8. INTERACTIVE PLOTLY GRAPH OF ESTIMATES WITH ERROR BARS
# ============================================================
mygraph <- plot_ly(
data = filtereddata,
x = ~Percent_E,
y = ~reorder(Division, Percent_E),
type = 'scatter',
mode = 'markers',
error_x = list(
type = "data",
array = ~Percent_M,
visible = TRUE
),
marker = list(color = "#099d91", size = 10)
) %>%
layout(
title = "Estimates by Area (Interactive)",
xaxis = list(title = "2019–2023 ACS Estimate"),
yaxis = list(title = "", automargin = TRUE)
)
mygraph