Geospatial analysis and mapping are some of the fastest growing applications of the R programming language. Talented developers are making regular updates to key R packages, such as sf and ggplot2, that improve the speed and ease of manipulating and displaying spatial data. As the R community at the Urban Institute grows, we have built a number of tools and packages to help the research staff analyze and visualize their data in better and easier ways. Last year, we published our urbnmapr package, which enables researchers to easily create state and county maps in R, and our urbnthemes package, which enables users to style their graphs according to Urban’s data visualization style guide.
Along with the rest of the R community, we also continue to develop and enhance these R packages. Some of these developments are small, such as updating documentation, and some are major, such as incorporating support for different features. The R package format means that these updates have been seamless, and Urban researchers (and other package users) can simply reinstall the package to take advantage of the new features.
We have made three important changes from when we first published the urbnmapr package:
1. Simple features. Simple features are a recent major geospatial innovation (PDF), and they are one of the main tools making geospatial analysis more accessible and reproducible across programming languages.
A ‘feature’ is an object in space. Features have geometries, which describe their location and extent. For example, if you have a dataset of all the parks in Washington, DC, each park is a feature, and the geometry of the parks is their location, shape, and size. Simple features are a new, internationally recognized standard for representing and encoding these feature geometries.
In R, simple features are implemented through the sf package. An “sf object” is a dataframe that contains a column (usually called “geometry”) with the geographic information needed to map the object. As opposed to prior spatial data formats, such as sp, a simple feature will look and act like a normal dataframe most of the time, so you don’t have to learn complex new techniques to do simple cleaning and manipulating. This means you can now use your favorite tidyverse functions on spatial data with the same syntax — like using
`filter()` to subset your data or using
`summarize()` to aggregate up to a different geography.
With the new sf option, the urbnmapr workflow remains the same, but is now easier and more intuitive to use. First, install and load the urbnmapr package by using the devtools package:
Second, use the get_urbn_map() function to load the shapes for US states and counties (to date, we have not extended the function to other areas around the world). With the geographies loaded into R, you can then merge this sf dataframe with your data (and make sure to set the new
`sf =` argument to “TRUE”).
Lastly, you can use ggplot2 to map the data, using geom_sf().
2. Territories. The get_urbn_map() function now has enhanced options for including US territories in both the state and county maps. These additional areas can be added to your maps by simply specifying a different option in the get_urbn_map() function. For instance, our colleagues in the Center on Labor, Human Services, and Population needed the new territories format to map home-visiting services (PDF) for expectant parents and parents of new children, which we could easily accomplish in the new package syntax:
3. urbnthemes. urbnthemes is another R package, built and maintained by my colleagues Aaron Williams and Kyle Ueyama, that provides tools for styling graphics made in R according to Urban’s data visualization style guide. urbnmapr and urbnthemes are both in constant development, and the packages continue to work together with these new updates. There are default options in urbnthemes that make producing publication-quality maps in R painless.
As with urbnmapr, you need to use the devtools package to install the urbnthemes package.
The set_urbn_defaults() function has an option specifically for maps.
Our goal is to keep making mapping and geospatial analysis easier and more accessible for Urban’s researchers. We are developing new guides and starting to lead trainings on this topic. Our goal is for urbnmapr to evolve with the R geospatial ecosystem, so we plan to continually update the package to help Urban researchers and others to easily produce US-based maps.