# Installing required packages
if (!require("tidyverse")) install.packages("tidyverse")
## Loading required package: tidyverse
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.9
## ✔ tidyr 1.2.0 ✔ stringr 1.4.0
## ✔ readr 2.1.2 ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
if (!require("tidycensus")) install.packages("tidycensus")
## Loading required package: tidycensus
if (!require("sf")) install.packages("sf")
## Loading required package: sf
## Linking to GEOS 3.9.1, GDAL 3.4.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(tidycensus)
library(tidyverse)
library(sf)
options(tigris_use_cache = TRUE)
# NOTE: Obtain a Census API key from
# https://api.census.gov/data/key_signup.html
# and replace the census_api_key code line's
# PastYourAPIKeyBetwenTheseQuoteMarks text with your
# API key. Also, delete the "#" from the front of the
# census_api_key code.
#census_api_key("PasteYourAPIKeyBetwenTheseQuoteMarks")
# Getting ACS variable info
# Change "2021" to your preferred year.
# Change "acs1" to "acs5" if you want five-year data.
DetailedTables <- load_variables(2021, "acs1", cache = TRUE)
SubjectTables <- load_variables(2021, "acs1/subject", cache = TRUE)
ProfileTables <- load_variables(2021, "acs1/profile", cache = TRUE)
AllVariables <- rbind(DetailedTables,SubjectTables,ProfileTables)
rm(DetailedTables,SubjectTables,ProfileTables)
# When you have the name of the variable you want, paste it
# in place of "B01001_001" in both instances below.
# Replace "TN" with a different two-letter state
# abbreviation to get data for that state's counties.
# Getting the map data
MapData <- get_acs(geography = "county",
state = "TN",
variables = c(Pop = "B01001_001",
Age = "B01002_001",
AgeMale = "B01002_002",
AgeFemale = "B01002_003",
White = "DP05_0037P",
Black = "DP05_0038P",
College = "DP02_0068P",
HHIncome = "S1903_C03_001",
PctMale = "DP05_0002P"),
year = 2021,
survey = "acs1",
output = "wide",
geometry = TRUE)
## Getting data from the 2021 1-year ACS
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Fetching data by table type ("B/C", "S", "DP") and combining the result.
#Pruning any empty columns
MapData <- MapData[colSums(!is.na(MapData)) > 0]
# Making preview maps
options(scipen=999)
MapData %>%
ggplot(aes(fill = PopE)) +
geom_sf(color = NA) +
scale_fill_viridis_c(option = "magma")

MapData %>%
ggplot(aes(fill = AgeE)) +
geom_sf(color = NA) +
scale_fill_viridis_c(option = "magma")

MapData %>%
ggplot(aes(fill = HHIncomeE)) +
geom_sf(color = NA) +
scale_fill_viridis_c(option = "magma")

MapData %>%
ggplot(aes(fill = WhiteE)) +
geom_sf(color = NA) +
scale_fill_viridis_c(option = "magma")

MapData %>%
ggplot(aes(fill = CollegeE)) +
geom_sf(color = NA) +
scale_fill_viridis_c(option = "magma")

# The code below will export a .kml and .csv file
# to your computer's hard drive. Look for the files
# in the same subdirectory as this R script. Columns
# containing only "NA"codes will be deleted.
#Making a .kml Map File
st_write(MapData,"MapData.kml", append = FALSE)
## Writing layer `MapData' to data source `MapData.kml' using driver `KML'
## Writing 20 features with 19 fields and geometry type Multi Polygon.
#Making a CSV data file
CSVData <- st_drop_geometry(MapData)
write.csv(CSVData,"CSVData.csv", row.names = FALSE)