library(data.table)
library(ggplot2)
library(magrittr)
library(devtools)
library(here)
library(kableExtra)
library(RColorBrewer)
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:
Load the package deandevl/RcensusPkg
from github and define a directory to receive geometric shapefiles downloaded from the Census Bureau.
::install_github('deandevl/RcensusPkg')
devtools::install_github('deandevl/RspatialPkg')
devtools<- file.path(here(), "r_packages", "RcensusPkg", "demos", "shapefiles") output_dir
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:
<- RcensusPkg::get_vintage_data(
housing_values_dt dataset = "acs/acs1",
vintage = 2023,
vars = "B25077_001E",
region = "state:*"
%>%
) ::setnames(old = "B25077_001E", new = "estimate") %>%
data.table:= as.numeric(estimate)] %>%
.[, estimate ::setnames(., old = "estimate", new = "Median_House_Values") data.table
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
<- classInt::classIntervals(
intervals $Median_House_Values,
housing_values_dtn = 6,
style = "pretty"
)<- intervals$brks
breaks <- c("$100,000","$200,000","$300,000","$400,000","$500,000","$600,000","$700,000", "$800000", "$900000")
labels
# Plot the map of US housing values
::plot_us_data(
RcensusPkgdf = 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”:
<- RcensusPkg::get_variable_names(
DP02_0068P_dt dataset = "acs/acs5/profile",
vintage = 2020,
filter_name_str = "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:
<- usmap::fips(state = "kentucky")
kentucky_fips <- RcensusPkg::get_vintage_data(
bachelors_dt dataset = "acs/acs5/profile",
vintage = 2020,
vars = "DP02_0068PE",
region = "county:*",
regionin = paste0("state:", kentucky_fips)
%>%
) ::setnames(old = "DP02_0068PE", new = "Percent_Bachelor_Higher") %>%
data.table:= as.numeric(Percent_Bachelor_Higher)] .[, Percent_Bachelor_Higher
Third, from the Census Bureau “tiger” datasets get the geometric Kentucky county boundaries and join it with the above education data:
<- expression(STATEFP == kentucky_fips)
express <- RcensusPkg::tiger_counties_sf(
bachelors_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():
::get_geom_sf(
RspatialPkgsf = 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
)