Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Today a coworker needed some maps showing boundary changes. I used what I learned last week in the useR 2012 geospatial data course to make a few simple maps in R, overlaid on OpenStreetMap tiles. I’m posting my maps and my R code in case others find them useful.
U.S. Census Bureau geography can take a while to understand, and the boundaries used (for organizing data collection and for publishing tabulations) can change over time as the population shifts. The idea behind these maps was to illustrate some ways that Census block-groups can change from one decennial census to the next.
- In the example above, parts of three different block-groups from 2000 (green, orange, pink) were combined into one 2010 block-group (thick purple line).
- In the map below, the block-groups mostly align (red and blue), but part of a 2000 block-group was chopped off into a new block (black dotted line) in a different 2010 block-group.
The map boundaries are defined in shapefiles, which are publicly available for download at the 2010 TIGER/Line Shapefiles website. Select “Blocks” or “Block Groups,” then the state (Alabama), then the county (Autauga or Mobile) to download a zip file containing several geographic files. Unzip them all into the same directory. You’ll also need to install the R packages sp
, rgdal
, and OpenStreetMap
.
Then grab my R code, assign your own working directory to mydir
, and you should be good to go:
- I read in the shapefiles (from the unzipped folder, the file with .shp at the end);
- subset them to just the boundaries I need;
- find the bounding box that just barely contains all the shapes;
- download and draw the OpenStreetMap tiles for that bounding box;
- and plot the boundaries on top, trying to use diverging colors and line types/widths so that the boundaries are distinguishable even if printed in greyscale.
For future work:
- After coding all this, I discovered a suite of
UScensus
R packages which apparently contain (or help to download?) spatial data for several levels of Census geography. These may provide an easier way to get the shapefiles, but I have yet to try them out. - There must be a simpler way to get a bounding box big enough for all the block groups at once. I tried to combine the shapefiles together using
rbind
, but it didn’t work, perhaps because some of the variable names differ from Census 2000 to Census 2010 or from blocks to block-groups. So instead, I am getting the bounding box for each area separately, then find the max & min of the lats & longs across all areas.
Map data © OpenStreetMap contributors, CC BY-SA.
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.