Before you start

In previous chapters, we have shown how to create very simple maps quickly from vector and raster data. This section focuses on using the ggplot2 package to create high-quality maps that are publishable in journal articles, conference presentations, and any kind of professional reports. This requires fine-tuning the aesthetics of maps beyond default maps, such as using the appropriate color scheme, removing unnecessary information, formatting legends, etc. This chapter focuses on creating static maps, and does not cover interactive maps that you often see on the web.

Creating maps differs from creating non-spatial figures in some ways. However, the underlying principle and syntax under ggplot2 to create maps and non-spatial figures are very similar. Indeed, you will find map making very intuitive and rather easy if you already have some knowledge of how ggplot2 works even if you have not created maps using ggplot2 before. Indeed, the only major difference between them is the choice of geom_*() types. We have several geom_*() types at our disposal for spatial data visualization.

  • geom_sf() for sf (vector) objects
  • geom_raster() for raster data
  • geom_stars() for stars (both vector and raster) object

These geom_*()s allow for visualizing both vector and raster data through consistent and simple ggplot2 syntax. It provides direct supports to sf and stars objects, meaning that no transformation of those objects is necessary prior to creating maps. On the other hand, (a very simple) data transformation is necessary for Raster\(^*\) objects by the raster package and SpatRaster or SpatVector by the terra package. We will look at each of the geoms individually to understand their basic usage below in sections 8.1 and 8.2. You will notice that there is nothing spatial about the sections following these sections. They are general and applicable to any kind of figures.

Note : While this chapter does not assume much knowledge of ggplot2, the basic knowledge of ggplot2 is extremely helpful. If you do not know anything about ggplot2 or you are afraid that your knowledge of ggplot2 is insufficient, Appendix B provides minimal knowledge of data visualization using the ggplot2 package so you can at least understand what is happening in this Chapter.


Useful resources

As mentioned earlier, general knowledge of how ggplot2 works is very useful. So, any resources for learning ggplot2 are useful. Some of them are:

The following book provides numerous map making examples using ggplot2. It is a good place to further improve your map making skills after completing this chapter.

Direction for replication

Datasets

All the datasets that you need to import are available here. In this chapter, the path to files is set relative to my own working directory (which is hidden). To run the codes without having to mess with paths to the files, follow these steps:

  • set a folder (any folder) as the working directory using setwd()
  • create a folder called “Data” inside the folder designated as the working directory (if you have created a “Data” folder previously, skip this step)
  • download the pertinent datasets from here
  • place all the files in the downloaded folder in the “Data” folder

Packages

  • Run the following code to install or load (if already installed) the pacman package, and then install or load (if already installed) the listed package inside the pacman::p_load() function.
if (!require("pacman")) install.packages("pacman")
pacman::p_load(
  stars, # spatiotemporal data handling
  raster, # raster data handling
  terra, # raster data handling
  sf, # vector data handling
  dplyr, # data wrangling
  stringr, # string manipulation
  lubridate, # dates handling
  data.table, # data wrangling
  patchwork, # arranging figures
  tigris, # county border
  colorspace, # color scale
  viridis, # arranging figures
  tidyr, # reshape
  ggspatial, # north arrow and scale bar
  ggplot2 # make maps
)