Sections

1. Thematic Mapping with sf & ggplot::geom_sf: creating choropleth with ggplot2, sf, and viridis packages

2. Faceted Thematic Maps with ggplot2::geom_sf

3. Thematic Mapping with tmap: creating choropleth with the tmap package

Simple Features

The initial modules of Mapping in R introduce plotting latitude and longitude with an interactive map (mapview) and generating a choropleth (a thematic map) using tidycensus. The focus of this module is to introduce other customized thematic map packages: tmap, sf, ggplot2.

The backbone of this type of mapping data manipulation is the sf package – sf for simple features – a modern tidyverse compatible package for encoding spatial vector data. sf is the successor to sp. Since sf is tidyverse compatible, among the advantages is the fact that you can see your numeric data in a more familiar (and easier-to-view) data frame .

The sf simple features package affords the R coder an easy and familiar method of storing data vectors and shapefile polygons in a tidy data frame.

In these Thematic Mapping with Simple Features sections, we introduce the tmap package first. Through the the tmap package you will learn an easy-to-use package for building spatial distributions on geographic maps. These maps are typically .png image files. (.png files are easy to produce and share.) In this section we’ll also use the tigris package – to gather Census shapefiles – and store those shape objects in the Simple Features data structure. (In the Chropleth module we used tidycensus to gather census Geography shape objects, as well as to gather census data variables.) tigris simply gathers the shapefile polygons. Later we will join the polygon object to a data frame using tmaptools.

Next, we’ll use the ggplot::geom_sf function to create our mapping visualizations using the widely adopted ggplot2 syntax.

Later, in the interactive thematic mapping section, you will make a different interactive choropleth using leaflet. This section is largely a repeat of the earlier choropleth module. The level of interactivity is sparse, but functional. Interactivity demonstrates how you can generate maps which move beyond traditional 2D map-on-a-page images. Just know that this zoom-in/zoom-out interactivity feature is possible because we use leaflet to draw the map. By extension, you can add many interactive features, e.g. pop-up windows. This section is a good set-up for the last section which covers how to save maps as files.

Saving / sharing ouput files. Even interactive maps can be shared and distributed. You can share maps either on the web, as an HTML file.

Converting between sf and sp

sf is developed by the same person/people who developed sp. sf is the modern approach. However, if you need to convert between the two formats, it’s easy! See Reading, Writing and Converting Simple Features