Site icon R-bloggers

New in the tigris package: simple features support and historic shapefiles

[This article was first published on Kyle Walker, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I am excited to announce that tigris 0.5 is now on CRAN. This is a major release that has been in the works for several months. Get it with install.packages("tigris").

One major new feature is support for the simple features data model via the sf R package. sf allows for the representation of spatial objects in R like data frames, but with a list-column containing feature geometry. This has multiple advantages, as objects of class sf accept tidyverse functions for data wrangling and are much faster to work with. For tigris, this means that large objects like Census blocks or national railroads are much more usable in R.

To return an object of class sf via tigris, either supply the argument class = "sf" or set this globally in your R session with options(tigris_class = "sf"). For example:

library(tigris)

options(tigris_class = "sf")

co <- counties(cb = TRUE)

head(co)
## Simple feature collection with 6 features and 9 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -88.47323 ymin: 31.18113 xmax: -85.04931 ymax: 33.00682
## epsg (SRID):    4269
## proj4string:    +proj=longlat +datum=NAD83 +no_defs
##   STATEFP COUNTYFP COUNTYNS       AFFGEOID GEOID    NAME LSAD      ALAND
## 1      01      005 00161528 0500000US01005 01005 Barbour   06 2291820706
## 2      01      023 00161537 0500000US01023 01023 Choctaw   06 2365954971
## 3      01      035 00161543 0500000US01035 01035 Conecuh   06 2201896058
## 4      01      051 00161551 0500000US01051 01051  Elmore   06 1601876535
## 5      01      065 00161558 0500000US01065 01065    Hale   06 1667804583
## 6      01      109 00161581 0500000US01109 01109    Pike   06 1740741211
##     AWATER                       geometry
## 1 50864677 MULTIPOLYGON(((-85.748032 3...
## 2 19059247 MULTIPOLYGON(((-88.473227 3...
## 3  6643480 MULTIPOLYGON(((-87.427204 3...
## 4 99850740 MULTIPOLYGON(((-86.413335 3...
## 5 32525874 MULTIPOLYGON(((-87.870464 3...
## 6  2336975 MULTIPOLYGON(((-86.199408 3...

geo_join and rbind_tigris still work with objects of class sf; however, you can use the *_join functions from the tidyverse as well.

The second major new feature is support for historic shapefiles going back to 1990. Historical data are available for the states, counties, tracts, and block_groups functions, and I’ve re-factored much of tigris’s code to easily accommodate other geographies if requested by users.

library(purrr)

options(tigris_class = "sp")

par(mfrow = c(1, 3))

walk(c(1990, 2000, 2010), function(year) {
  plot(tracts("TX", "Denton", cb = TRUE, year = year), 
       main = paste0("Census tracts in Denton County, ", 
                     as.character(year)))
})

My motivation for these updates is twofold. First, I’m working on a new package called tidycensus that will integrate tigris with the decennial Census and American Community Survey APIs. This package will allow R users to return integrated spatial and demographic datasets as sf objects in one function call, and will be designed to work seamlessly within the tidyverse. Second, I’m starting a book project this summer called Analyzing the US Census with R. The book will cover Census data acquisition and wrangling; spatial modeling with Census data; and working with Census microdata from the Integrated Public Use Microdata Series, all within R. Look for updates on both projects in this space!

If you are interested in supporting the development of these projects, I encourage you to hire me for a workshop on these topics or to consult on your next project. Check out the Services page for information on how we can get started.

To leave a comment for the author, please follow the link and comment on their blog: Kyle Walker.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.