tigris
is an R package I am developing to allow R users to download and use 2014 TIGER/Line shapefiles. The package is under active development, and the API is subject to change. The code is available on GitHub at https://github.com/walkerke/tigris; please fork and contribute if you’d like!
To install the package, issue the following command:
devtools::install_github('walkerke/tigris')
Using the package is as simple as calling a function that corresponds to the geography you’d like to download. tigris
will download the TIGER 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(magrittr)
states() %>% plot()
Many geographies are only available at state or county levels, so you’ll need to supply the corresponding state and county FIPS codes to get the data. Codes for states and counties can be accessed with the built-in lookup_code
function. For example, let’s say that we want to use Census tract data for Dallas and Tarrant Counties in Texas.
lookup_code("Texas", "Dallas")
## [1] "The code for Texas is '48' and the code for Dallas County is '113'."
lookup_code("Texas", "Tarrant")
## [1] "The code for Texas is '48' and the code for Tarrant County is '439'."
Now that we have the appropriate state and county codes, we can fetch the data by supplying the state FIPS code for Texas and an appropriate vector of county codes. We also can supply the argument detailed = FALSE
to return a simplified (and faster to download) dataset which is suitable for mapping.
dfw <- tracts(state = '48', county = c('113', '439'), detailed = FALSE)
plot(dfw)
Of course, once you have the shape data, you probably 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,
popup = popup) %>%
addLegend(pal = pal,
values = dfw_merged$hhincome,
position = "bottomright",
title = "Income in DFW")
Please follow the project GitHub website (https://github.com/walkerke/tigris) for updates!