tigris is an R package that I have developed in collaboration with Bob Rudis to allow R users to quickly download and use TIGER/Line shapefiles.

To install the package from CRAN, issue the following command:

install.packages('tigris')

Using the package is as simple as calling a function that corresponds to the geography you’d like to download. Many TIGER/Line shapefiles are available from the Census Bureau by state and/or by county; tigris is smart about handling state/county FIPS codes and allows you to supply their names instead. In one line of code, tigris will download a TIGER/Line shapefile from the Census Bureau for you, and read it into R with readOGR from the rgdal package to return a spatial data frame.

library(tigris)
library(sp)

dfw <- tracts(state = 'TX', county = c('Dallas', 'Tarrant'))

plot(dfw)

tigris defaults to the 2014 TIGER/Line shapefiles; however, other years are available as well. For example, if you want a 2013 shapefile, supply the argument year = 2013 to your function. To get 2013 shapefiles throughout your R session, enter the command options(tigris_year = 2013) tigris also caches shapefile downloads, so you don’t have to re-download every time!

In addition to the core TIGER/Line shapefiles, tigris grants access to generalized cartographic boundary files available from the Census Bureau. Generalized files are not available for every Census geography; however the smaller size of these files can make them better for mapping. To get these files (when available), supply the argument cb = TRUE. Certain geographies also have very-generalized versions available; these can be accessed by supplying '5m' or '20m' to the resolution parameter (when cb = TRUE). For example:

tx_counties <- counties(state = 'TX', cb = TRUE, resolution = '20m')

plot(tx_counties)

tigris also includes a built-in function, rbind_tigris, that allows users to combine a list of multiple downloads of the same type into a single Spatial DataFrame. For example, roads are available by county, but a user may want to retrieve roads for multiple counties.

wtx_counties <- c('Loving', 'Winkler', 'Ward')

wtx_roads <- rbind_tigris(
  lapply(
    wtx_counties, function(x) roads(state = 'TX', county = x)
  )
)

plot(wtx_roads)

Once you have shape data, you may want to do more than just plot the outlines. The package includes a helper function, geo_join, that handles the sometimes-messy process of joining tabular data to shape data in R. Let’s grab median household income data by Census tract for the two counties using the acs package, and map it using the leaflet package:

library(acs)
library(leaflet)

# api.key.install("my_key_here") You can get your own API key from the Census Bureau

income_data <- acs.fetch(endyear = 2012, 
                         geography = geo.make(state = "TX", 
                                              county = c(113, 439), 
                                              tract = "*"), 
                         variable = "B19013_001")

income_df <- data.frame(paste0(as.character(income_data@geography$state), 
                               as.character(income_data@geography$county), 
                               income_data@geography$tract), 
                        income_data@estimate)

colnames(income_df) <- c("GEOID", "hhincome")

dfw_merged <- geo_join(dfw, income_df, "GEOID", "GEOID")

pal <- colorQuantile("Greens", NULL, n = 6)

popup <- paste0("Median household income: ", as.character(dfw_merged$hhincome))

leaflet() %>%
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(data = dfw_merged, 
              fillColor = ~pal(dfw_merged$hhincome), 
              fillOpacity = 0.7, 
              weight = 0.2, 
              smoothFactor = 0.2, 
              popup = popup) %>%
  addLegend(pal = pal, 
            values = dfw_merged$hhincome, 
            position = "bottomright", 
            title = "Income in DFW")

tigris also fits well within common spatial analysis workflows in R by giving R users direct access to the Census shapefiles. For example, let’s figure out which of our Census tracts intersect Benbrook Lake in the Fort Worth area, available from the area_water() function in tigris. We’ll be using spatial overlay functions from the rgeos package to accomplish this.

library(rgeos)

tarrant_water <- area_water("TX", "Tarrant")

benbrook <- gUnaryUnion(tarrant_water[grep('Benbrook', tarrant_water$FULLNAME), ])

bb_tracts <- dfw[as.vector(gIntersects(dfw, benbrook, byid = TRUE)), ]

leaflet(bb_tracts) %>% addTiles() %>% addPolygons()

Try out the package for yourselves! Also, you can follow the project GitHub page at https://github.com/walkerke/tigris for updates.