Load libraries and census data
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ 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
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.3.3
census_api_key("1ef8386987d5b937e863ac8b0ba1273ff9de5ec0")
## To install your API key for use in future sessions, run this function with `install = TRUE`.
v22 <- load_variables(2022, "acs5", cache = TRUE)
Load Florida South Asian population data:
fl <- get_acs(geography = "county",
variables = c(
tot_pop = "B01003_001",
asian_indian = "B02001_004",
bangladeshi = "B02001_005",
pakistani = "B02001_006",
sri_lankan = "B02001_007"
), state = "FL", geometry = TRUE, year = 2022)
## 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)`.
Transform the data:
fl_wider <- fl |>
select(-moe) |>
pivot_wider(names_from = variable, values_from = estimate) |>
mutate(
south_asian_total = asian_indian + bangladeshi + pakistani + sri_lankan,
south_asian_pct = south_asian_total / tot_pop)
Plot the data:
fl_wider |>
ggplot(aes(fill = south_asian_pct)) +
geom_sf(color = NA) +
scale_fill_viridis_c(option = "mako")
Load Florida number of naturalized citizens data:
fl_nt_citizens <- get_acs(geography = "county",
variables = c(tot_pop = "B01003_001", nat_citz = 'B05005_002'), state = "FL", geometry = TRUE, year = 2022)
## 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)`.
Mutate to find percentage:
fl_wider_NC <- fl_nt_citizens |>
select(-moe) |>
pivot_wider(names_from = variable, values_from = estimate) |>
mutate(
nat_pct = nat_citz / tot_pop)
Plot the data:
fl_wider_NC |>
ggplot(aes(fill = nat_pct)) +
geom_sf(color = NA) +
scale_fill_viridis_c(option = "mako")