This is my Day 1 chart for the #30DayChartChallenge. Today’s prompt is Comparisons: Fractions.
This RMarkdown demonstrates my approach of downloading Census tract data for Fulton, DeKalb, and Clayton Counties in Georgia, calculating the percent minority population, and mapping it with tmap.
# 1. Load necessary libraries
library(tidyverse)
library(tidycensus)
library(sf)
library(tmap)
# 2. Set Census API key
census_api_key(Sys.getenv("Census_API"), install = FALSE)
# 3. Fetch Census data
census <- get_acs(geography="tract",
state="GA",
county=c("Fulton", "DeKalb", "Clayton"),
variables = c(totpop = 'B03002_001', black = 'B03002_004', asian = 'B03002_006', hispanic = 'B03002_012'),
year = 2022,
survey = "acs5",
# geometry = TRUE,
output = "wide",
progress = FALSE,
) %>%
mutate(pct_minority = ((blackE+asianE+hispanicE)/totpopE)*100)
ga_tracts <- st_read("tl_2022_13_tract/tl_2022_13_tract.shp")
## Reading layer `tl_2022_13_tract' from data source
## `/Users/katherinelosada/MSUA/tl_2022_13_tract/tl_2022_13_tract.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 2796 features and 12 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -85.60516 ymin: 30.35576 xmax: -80.78296 ymax: 35.0013
## Geodetic CRS: NAD83
ga_tracts_subset <- ga_tracts %>%
filter(COUNTYFP %in% c("121", "089", "063")) # Filter by county FIPS codes (Fulton=121, DeKalb=089, Clayton=063)
joined_data <- ga_tracts_subset %>%
left_join(census, by = "GEOID")
census <- joined_data %>%
st_transform(crs = 4326) %>%
separate(col = NAME.y, into = c("tract", "county", "state"), sep = "; ")
# 4. Create a choropleth map
tmap_mode("view")
tm_shape(census) +
tm_polygons(
col = "pct_minority",
border.col = "grey",
lwd = 0.4,
style = "fixed",
breaks = c(0, 20, 40, 60, 80, 100),
labels = c("0-20%", "20-40%", "40-60%", "60-80%", "80-100%"),
palette = "BuPu",
title = "Percent Minority",
id = "pct_minority",
popup.vars = c(
"Tract GEOID" = "GEOID",
"Percent Minority" = "pct_minority"
)
) +
tm_layout(
title = "Percent of Minority Population per Tract in Atlanta, GA",
frame = FALSE,
legend.outside = TRUE
)