Mapping demographic data with ggplot2

1. Mapping demographic data with ggplot2

In the previous lesson, you learned how to work with simple feature geometry and tidycensus data and made some simple maps using the base plot() function. As simple features are also supported in ggplot2 through its geom_sf() function, you can map tidycensus data with all the plotting power of ggplot2.

2. A basic choropleth map with geom_sf()

Shape fills can be styled with ggplot2 by using the fill aesthetic. In the example shown here, we map the estimate column - representing median home values by Census tract in Cook County - to the fill aesthetic and then use geom_sf() to plot the result.

3. A basic choropleth map

While the map is functional, it has some issues of clarity. Differences between tracts are difficult to distinguish on the color ramp, and the grey tract borders obscure these color differences in denser parts of the Chicago area.

4. Modifying map colors

Fortunately, we can customize the map using ggplot2's suite of plotting functions. In the original aesthetic call, we can set the color to the estimate column as well so that tract borders are colored in the same way as the tract fills. We'll then use the viridis color palette to style our data, which is now integrated into ggplot2. viridis palettes are perceptually uniform and are legible both in black and white and to colorblind individuals, which has made them quite popular for data visualization. We can see the difference in the result, as tracts are now much more distinct from one another on the map.

5. Customizing the map output

Before sharing our map, we'll now want to do some cleanup using ggplot2. This includes specifying an informative title, subtitle, caption, and legend title, setting the background to white with no gridlines using theme_minimal() and datum equals NA, and formatting the legend labels using the scales package.

6. Customizing the map output

And here is our result.

7. Let's practice!

Now, let's try making some maps of ACS data with tidycensus and ggplot2 in R.