Published

November 16, 2024

Demos for deandevl/RcensusPkg

The following scripts are a sampling of 2 US Census Bureau API requests from the package’s 39 demos.

Load the required R Packages from CRAN:

library(data.table)
library(ggplot2)
library(magrittr)
library(devtools)
library(here)
library(kableExtra)
library(RColorBrewer)

Load the package deandevl/RcensusPkg from github and define a directory to receive geometric shapefiles downloaded from the Census Bureau.

devtools::install_github('deandevl/RcensusPkg')
devtools::install_github('deandevl/RspatialPkg')
output_dir <- file.path(here(), "r_packages", "RcensusPkg", "demos", "shapefiles")

RcensusPkg::get_vintage_data

The first Census Bureau API request is for US house values (acronym “B25077_001E) for the year 2023 across all the states. We start with getting the housing values data:

housing_values_dt <- RcensusPkg::get_vintage_data(
  dataset = "acs/acs1",
  vintage = 2023,
  vars = "B25077_001E",
  region = "state:*"
) %>%
  data.table::setnames(old = "B25077_001E", new = "estimate") %>%
  .[, estimate := as.numeric(estimate)] %>%
  data.table::setnames(., old = "estimate", new = "Median_House_Values")
Housing values for the first 8 states
NAME Median_House_Values state GEOID
Alabama 216600 01 01
Alaska 347500 02 02
Arizona 411200 04 04
Arkansas 195700 05 05
California 725800 06 06
Colorado 550300 08 08
Connecticut 367800 09 09
Delaware 359700 10 10

The next step is to plot the values on a US choropleth map of the states. We will use the RcensusPkg::plot_us_data() function.

# Define "pretty" intervals for housing values
intervals <- classInt::classIntervals(
  housing_values_dt$Median_House_Values,
  n = 6,
  style = "pretty"
)
breaks <- intervals$brks
labels <- c("$100,000","$200,000","$300,000","$400,000","$500,000","$600,000","$700,000", "$800000", "$900000")

# Plot the map of US housing values
RcensusPkg::plot_us_data(
  df = housing_values_dt,
  title = "US Housing Values",
  states_col = "NAME",
  value_col = "Median_House_Values",
  output_dir = output_dir,
  scale_breaks = breaks,
  scale_limits = c(100000, 900000),
  scale_labels = labels,
  scale_colors = RColorBrewer::brewer.pal(8,"YlOrRd"),
  show_legend = TRUE,
  legend_pos = "top",
  legend_key_width = 3.0
)

RcensusPkg::tiger_counties_sf

In addition to downloading Census Bureau data, we can also download geometric boundaries for states, counties, blocks, tracts, urban areas, water, roads, landmarks, places, and zip code areas. In this example we will map the percent of a bachelor’s education or higher across counties in the state of Kentucky.

First get the Census Bureau’s description of the acronym “DP02_0068P”:

DP02_0068P_dt <- RcensusPkg::get_variable_names(
  dataset = "acs/acs5/profile",
  vintage = 2020,
  filter_name_str = "DP02_0068P"
)
A description of “DP02_0068P”
name label
DP02_0068PE Percent!!EDUCATIONAL ATTAINMENT!!Population 25 years and over!!Bachelor's degree or higher

Second, from the Census Bureau get the actual data across the Kentucky counties:

kentucky_fips <- usmap::fips(state = "kentucky")
bachelors_dt <- RcensusPkg::get_vintage_data(
  dataset = "acs/acs5/profile",
  vintage = 2020,
  vars = "DP02_0068PE",
  region = "county:*",
  regionin = paste0("state:", kentucky_fips)
) %>% 
  data.table::setnames(old = "DP02_0068PE", new = "Percent_Bachelor_Higher") %>% 
  .[, Percent_Bachelor_Higher := as.numeric(Percent_Bachelor_Higher)]

Third, from the Census Bureau “tiger” datasets get the geometric Kentucky county boundaries and join it with the above education data:

express <- expression(STATEFP == kentucky_fips)
bachelors_sf <- RcensusPkg::tiger_counties_sf(
  vintage = 2020,
  general = TRUE,
  output_dir = output_dir,
  sf_info = FALSE,
  datafile = bachelors_dt,
  datafile_key = "county",
  express = express
)

Finally, create a choropleth map of the degree of higher education across Kentucky counties using RspatialPkg::get_geom_sf():

RspatialPkg::get_geom_sf(
  sf = bachelors_sf,
  aes_fill = "Percent_Bachelor_Higher",
  hide_x_tics = TRUE,
  hide_y_tics = TRUE,
  panel_color = "white",
  panel_border_color = "white",
  title = "Percent with bachelor or higher education in Kentucky counties (2020)",
  center_titles = TRUE
)

R packages