# Installing and loading required packages
if (!require("tidyverse")) install.packages("tidyverse")
## Loading required package: tidyverse
## Warning: package 'tidyverse' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
if (!require("tidycensus")) install.packages("tidyverse")
## Loading required package: tidycensus
if (!require("sf")) install.packages("tidyverse")
## Loading required package: sf
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
if (!require("mapview")) install.packages("tidyverse")
## Loading required package: mapview
## Warning: package 'mapview' was built under R version 4.3.3
library(tidyverse)
library(tidycensus)
library(sf)
library(mapview)
# Transmitting API key
census_api_key("9bb13da5952acaea0094dc49530af428cb339cbd")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
# Fetching ACS codebooks
DetailedTables <- load_variables(2022, "acs5", cache = TRUE)
SubjectTables <- load_variables(2022, "acs5/subject", cache = TRUE)
ProfileTables <- load_variables(2022, "acs5/profile", cache = TRUE)
# Double checking target variables
ChosenVars <- filter(ProfileTables,name == "DP02_0154P"|
name == "DP02_0001")
print(ChosenVars$name)
## [1] "DP02_0001" "DP02_0154P"
print(ChosenVars$label)
## [1] "Estimate!!HOUSEHOLDS BY TYPE!!Total households"
## [2] "Percent!!COMPUTERS AND INTERNET USE!!Total households!!With a broadband Internet subscription"
print(ChosenVars$concept)
## [1] "Selected Social Characteristics in the United States"
## [2] "Selected Social Characteristics in the United States"
# Specifying target variables
VariableList =
c(PctGasHeat_ = "DP02_0154P",
Households_ = "DP02_0001")
# Fetching data
mydata <- get_acs(
geography = "county",
state = "TN",
variables = VariableList,
year = 2022,
survey = "acs5",
output = "wide",
geometry = TRUE)
## Getting data from the 2018-2022 5-year ACS
## Downloading feature geometry from the Census website. To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## Using the ACS Data Profile
##
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 10%
|
|======== | 11%
|
|========= | 12%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|============= | 18%
|
|============= | 19%
|
|============== | 20%
|
|=============== | 21%
|
|================ | 24%
|
|================= | 24%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|===================== | 30%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================== | 37%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 40%
|
|=============================== | 45%
|
|================================ | 46%
|
|===================================== | 53%
|
|====================================== | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|================================================ | 68%
|
|================================================= | 70%
|
|==================================================== | 74%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|============================================================= | 87%
|
|===================================================================== | 99%
|
|======================================================================| 100%
# Reformatting data
mydata <-
separate_wider_delim(mydata,
NAME,
delim = " County, ",
names = c("County", "State"))
# Filtering data
filtereddata <- mydata %>%
filter(County == "Davidson"|
County == "Rutherford"|
County == "Williamson"|
County == "Cheatham"|
County == "Robertson"|
County == "Sumner"|
County == "Wilson")
# Plotting data
ggplot(filtereddata, aes(x = PctGasHeat_E, y = reorder(County, PctGasHeat_E))) +
geom_errorbarh(aes(xmin = PctGasHeat_E - PctGasHeat_M, xmax = PctGasHeat_E + PctGasHeat_M)) +
geom_point(size = 3, color = "darkblue") +
theme_minimal(base_size = 12.5) +
labs(title = "Pct. households with gas heat",
subtitle = "Nashville-area counties. Brackets show error margins.",
x = "2018-2022 ACS estimate",
y = "")
# Mapping data
mapdata <- filtereddata %>%
rename(PctGasHeat = PctGasHeat_E,
Households = Households_E)
mapdata <- st_as_sf(mapdata)
mapviewOptions(basemaps.color.shuffle = FALSE)
mapview(mapdata, zcol = "PctGasHeat",
layer.name = "Pct. with gas heat",
popup = TRUE)
# Exporting data in .csv format
CSVdata <- st_drop_geometry(mapdata)
write.csv(CSVdata, "mydata.csv", row.names = FALSE)