The goal of getACS is to make it easier to work with American Community Survey data from the tidycensus package by Kyle Walker and others.
This package includes:
tidycensus::get_acs() to support multiple
tables, geographies, or yearsYou can install the development version of getACS from GitHub with:
# install.packages("pak")
# pak::pkg_install("elipousson/getACS")
install.packages("getACS", repos = c('https://elipousson.r-universe.dev', 'https://cloud.r-project.org'))
library(getACS)
library(gt)
library(ggplot2)
The main feature of {getACS} is support for returning multiple tables,
geographies, and years.
acs_data <- get_acs_geographies(
geography = c("county", "state"),
county = "Baltimore city",
state = "MD",
table = "B08134",
year = 2022,
quiet = TRUE
)
The package also includes utility functions for filtering data and
selecting columns to support the creation of tables using the {gt}
package:
tbl_data <- filter_acs(acs_data, indent == 1, line_number <= 10)
tbl_data <- select_acs(tbl_data)
commute_tbl <- gt_acs(
tbl_data,
groupname_col = "NAME",
column_title_label = "Commute time",
table = "B08134"
)
as_raw_html(commute_tbl)
| Commute time | Est. | % share |
|---|---|---|
| Baltimore city, Maryland | ||
| Maryland | ||
| Source: 2018-2022 ACS 5-year Estimates, Table B08134. | ||
The gt_acs_compare() function also allows side-by-side comparison of
geographies:
commute_tbl_compare <- gt_acs_compare(
data = tbl_data,
id_cols = "column_title",
column_title_label = "Commute time",
table = "B08134"
)
as_raw_html(commute_tbl_compare)
| Est. | % share | Est. | % share | |
|---|---|---|---|---|
| Source: 2018-2022 ACS 5-year Estimates, Table B08134. | ||||
gt_acs_compare_vars() is a variant on gt_acs_compare() where the
default values support comparisons with values in columns and
geographical areas in rows:
commute_tbl_compare_vars <- acs_data |>
filter_acs(indent == 1, line_number > 10) |>
gt_acs_compare_vars(
table = acs_data$table_id
)
as_raw_html(commute_tbl_compare_vars)
| NAME | Car, truck, or van | Public transportation (excluding taxicab) | Walked | Taxicab, motorcycle, bicycle, or other means |
|---|---|---|---|---|
| Source: 2018-2022 ACS 5-year Estimates, Table B08134. | ||||
The package also includes several functions to support creating plots
with the {ggplot2} package including geom_acs_col() and
labs_acs_survey():
plot_data <- acs_data |>
filter_acs(indent == 1, line_number > 10) |>
select_acs() |>
fmt_acs_county(state = "Maryland")
plot_data |>
ggplot() +
geom_acs_col(
fill = "NAME",
position = "dodge",
color = NA,
alpha = 0.75,
perc = TRUE,
errorbar_params = list(position = "dodge", linewidth = 0.25)
) +
scale_y_discrete("Means of transportation to work") +
scale_fill_viridis_d("Geography") +
labs_acs_survey(
.data = acs_data
) +
theme_minimal()

The geom_acs_col() function calls geom_acs_errorbar() (passing the
errorbar_params argument as additional parameters) and scale_x_acs()
or scale_y_acs() (depending on whether orientation = "y" or the
default value of NA).
For more information on working with Census data in R read the book Analyzing US Census Data: Methods, Maps, and Models in R (February 2023).
{tidycensus}
label_acs_metadata())