If requested, tidycensus can return simple feature geometry for geographic units along with variables from the decennial US Census or American Community survey. By setting geometry = TRUE in a tidycensus function call, tidycensus will use the tigris package to retrieve the corresponding geographic dataset from the US Census Bureau and pre-merge it with the tabular data obtained from the Census API. The following example shows median household income from the 2011-2015 ACS for Census tracts in Orange County, California:

options(tigris_use_cache = TRUE)

orange <- get_acs(state = "CA", county = "Orange", geography = "tract", 
                  variables = "B19013_001", geometry = TRUE)

## Simple feature collection with 6 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -117.968 ymin: 33.85418 xmax: -117.816 ymax: 33.93211
## epsg (SRID):    4269
## proj4string:    +proj=longlat +datum=NAD83 +no_defs
##         GEOID                                           NAME   variable
## 1 06059001304  Census Tract 13.04, Orange County, California B19013_001
## 2 06059001507  Census Tract 15.07, Orange County, California B19013_001
## 3 06059001903  Census Tract 19.03, Orange County, California B19013_001
## 4 06059011601 Census Tract 116.01, Orange County, California B19013_001
## 5 06059011718 Census Tract 117.18, Orange County, California B19013_001
## 6 06059021820 Census Tract 218.20, Orange County, California B19013_001
##   estimate   moe                       geometry
## 1    62681  6958 MULTIPOLYGON (((-117.9592 3...
## 2    74718  6773 MULTIPOLYGON (((-117.9065 3...
## 3    66667 14114 MULTIPOLYGON (((-117.968 33...
## 4    48622  6312 MULTIPOLYGON (((-117.933 33...
## 5    96632 11050 MULTIPOLYGON (((-117.855 33...
## 6   132986 12673 MULTIPOLYGON (((-117.8437 3...

Our object orange looks much like the basic tidycensus output, but with a geometry list-column describing the geometry of each feature, using the geographic coordinate system NAD 1983 (EPSG: 4269) which is the default for Census shapefiles. tidycensus uses the Census cartographic boundary shapefiles for faster processing; if you prefer the TIGER/Line shapefiles, set cb = FALSE in the function call.

As the dataset is in a tidy format, it can be quickly visualized with the geom_sf functionality currently in the development version of ggplot2:


orange %>%
  ggplot(aes(fill = estimate, color = estimate)) + 
  geom_sf() + 
  coord_sf(crs = 26911) + 
  scale_fill_viridis(option = "magma") + 
  scale_color_viridis(option = "magma")

Faceted mapping

One of the most powerful features of ggplot2 is its support for small multiples, which works very well with the tidy data format returned by tidycensus. Many Census and ACS variables return counts, however, which are generally inappropriate for choropleth mapping. In turn, get_decennial and get_acs have an optional argument, summary_var, that can work as a multi-group denominator when appropriate. Let’s use the following example of the racial geography of Harris County, Texas. First, we’ll request data for non-Hispanic whites, non-Hispanic blacks, non-Hispanic Asians, and Hispanics by Census tract for the 2010 Census, and specify total population as the summary variable. year is not necessary here as the default is 2010.

racevars <- c(White = "P0050003", 
              Black = "P0050004", 
              Asian = "P0050006", 
              Hispanic = "P0040003")

harris <- get_decennial(geography = "tract", variables = racevars, 
                  state = "TX", county = "Harris County", geometry = TRUE,
                  summary_var = "P0010001") 

## Simple feature collection with 6 features and 5 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -95.37457 ymin: 29.74486 xmax: -95.32409 ymax: 29.80907
## epsg (SRID):    4269
## proj4string:    +proj=longlat +datum=NAD83 +no_defs
## # A tibble: 6 x 6
##   GEOID   NAME    variable  value summary_value                   geometry
##   <chr>   <chr>   <chr>     <dbl>         <dbl>     <S3: sfc_MULTIPOLYGON>
## 1 482011~ Census~ White    2082            4690 MULTIPOLYGON (((-95.37348~
## 2 482011~ Census~ Black    1047            4690 MULTIPOLYGON (((-95.37348~
## 3 482011~ Census~ Asian     134            4690 MULTIPOLYGON (((-95.37348~
## 4 482011~ Census~ Hispanic 1070            4690 MULTIPOLYGON (((-95.37348~
## 5 482012~ Census~ White      35.0          1620 MULTIPOLYGON (((-95.32851~
## 6 482012~ Census~ Black    1195            1620 MULTIPOLYGON (((-95.32851~

We notice that there are four entries for each Census tract, with each entry representing one of our requested variables. The summary_value column represents the value of the summary variable, which is total population in this instance. When a summary variable is specified in get_acs, both summary_est and summary_moe columns will be returned.

With this information, we can set up an analysis pipeline in which we calculate a new percent-of-total column; recode the Census variable names into more intuitive labels; and visualize the result for each group in a faceted plot.

harris %>%
  mutate(pct = 100 * (value / summary_value)) %>%
  ggplot(aes(fill = pct, color = pct)) +
  facet_wrap(~variable) +
  geom_sf() +
  coord_sf(crs = 26915) + 
  scale_fill_viridis() +

Detailed shoreline mapping with tidycensus

Geometries in tidycensus default to the Census Bureau’s cartographic boundary shapefiles. Cartographic boundary shapefiles are preferred to the core TIGER/Line shapefiles in tidycensus as their smaller size speeds up processing and because they are pre-clipped to the US coastline.

However, there may be circumstances in which your mapping requires more detail. A good example of this would be maps of New York City, in which even the cartographic boundary shapefiles include water area. For example, take this example of median household income by Census tract in Manhattan (New York County), NY:

options(tigris_use_cache = TRUE)

ny <- get_acs(geography = "tract", 
              variables = "B19013_001", 
              state = "NY", 
              county = "New York", 
              geometry = TRUE)

mapview(ny, zcol = "estimate", legend = TRUE)

As illustrated in the graphic, the boundaries of Manhattan include water boundaries - stretching into the Hudson and East Rivers. In turn, a more accurate representation of Manhattan’s land area might be desired. To accomplish this, a tidycensus user can use the core TIGER/Line shapefiles instead, then erase water area from Manhattan’s geometry.

tidycensus allows users to get TIGER/Line instead of cartographic boundary shapefiles with the keyword argument cb = FALSE. This argument will be familiar to users of the tigris package, as it is used by tigris to distinguish between cartographic boundary and TIGER/Line shapefiles in the package.

ny2 <- get_acs(geography = "tract", 
              variables = "B19013_001", 
              state = "NY", 
              county = "New York", 
              geometry = TRUE, 
              cb = FALSE)

Next, tools in the tigris and sf package can be used to remove the water area from Manhattan’s Census tracts. sf allows users to “erase” one geometry from another, akin to tools available in desktop GIS software. The st_erase() function defined below is not exported by the package, but is defined in the documentation for st_difference().

The geometry used to “erase” water area from the tract polygons is obtained by the area_water() function in tigris, making sure to choose the option class = "sf".


st_erase <- function(x, y) {
  st_difference(x, st_union(st_combine(y)))

ny_water <- area_water("NY", "New York", class = "sf")

ny_erase <- st_erase(ny2, ny_water)

After performing this operation, we can visualize the result:

mapview(ny_erase, zcol = "estimate", legend = TRUE)

The map appears as before, but instead the polygons now hug the shoreline of Manhattan.

Writing to shapefiles

Beyond this, you might be interested in writing your dataset to a shapefile or GeoJSON for use in external GIS or visualization applications. You can accomplish this with the st_write function in the sf package:

st_write(orange, "orange.shp")

Your tidycensus-obtained dataset can now be used in ArcGIS, QGIS, Tableau, or any other application that reads shapefiles.

There is a lot more you can do with the spatial functionality in tidycensus, including more sophisticated visualization and spatial analysis; look for updates on my blog and in this space.