8.6 Other mapping packages

tmap provides a powerful interface for creating a wide range of static maps (Section 8.2) and also supports interactive maps (Section 8.4).But there are many other options for creating maps in R.The aim of this section is to provide a taster of some of these and pointers for additional resources: map making is a surprisingly active area of R package development, so there is more to learn than can be covered here.

The most mature option is to use plot() methods provided by core spatial packages sf and raster, covered in Sections 2.2.3 and 2.3.2, respectively.What we have not mentioned in those sections was that plot methods for raster and vector objects can be combined when the results draw onto the same plot area (elements such as keys in sf plots and multi-band rasters will interfere with this).This behavior is illustrated in the subsequent code chunk which generates Figure 8.25.plot() has many other options which can be explored by following links in the ?plot help page and the sf vignette sf5.

  1. g = st_graticule(nz, lon = c(170, 175), lat = c(-45, -40, -35))
  2. plot(nz_water, graticule = g, axes = TRUE, col = "blue")
  3. raster::plot(nz_elev / 1000, add = TRUE)
  4. plot(st_geometry(nz), add = TRUE)

Map of New Zealand created with plot(). The legend to the right refers to elevation (1000 m above sea level).
Figure 8.25: Map of New Zealand created with plot(). The legend to the right refers to elevation (1000 m above sea level).

Since version 2.3.0, the tidyverse plotting package ggplot2 has supported sf objects with geomsf().The syntax is similar to that used by tmap:an initial ggplot() call is followed by one or more layers, that are added with + geom(), where represents a layer type such as geom_sf() (for sf objects) or geom_points() (for points).

ggplot2 plots graticules by default.The default settings for the graticules can be overridden using scale_x_continuous(), scale_y_continuous() or coord_sf(datum = NA).Other notable features include the use of unquoted variable names encapsulated in aes() to indicate which aesthetics vary and switching data sources using the data argument, as demonstrated in the code chunk below which creates Figure 8.26:

  1. library(ggplot2)
  2. g1 = ggplot() + geom_sf(data = nz, aes(fill = Median_income)) +
  3. geom_sf(data = nz_height) +
  4. scale_x_continuous(breaks = c(170, 175))
  5. g1

Map of New Zealand created with ggplot2.
Figure 8.26: Map of New Zealand created with ggplot2.

An advantage of ggplot2 is that it has a strong user-community and many add-on packages.Good additional resources can be found in the open source ggplot2 book(Wickham 2016) and in the descriptions of the multitude of ‘ggpackages’ such as ggrepel and tidygraph.

Another benefit of maps based on ggplot2 is that they can easily be given a level of interactivity when printed using the function ggplotly() from the plotly package.Try plotly::ggplotly(g1), for example, and compare the result with other plotly mapping functions described at: blog.cpsievert.me.

At the same time, ggplot2 has a few drawbacks.The geom_sf() function is not always able to create a desired legend to use from the spatial data.Raster objects are also not natively supported in ggplot2 and need to be converted into a data frame before plotting.

We have covered mapping with sf, raster and ggplot2 packages first because these packages are highly flexible, allowing for the creation of a wide range of static maps.Before we cover mapping packages for plotting a specific type of map (in the next paragraph), it is worth considering alternatives to the packages already covered for general-purpose mapping (Table 8.1).

Table 8.1: Selected general-purpose mapping packages.
PackageTitle
cartographyThematic Cartography
ggplot2Create Elegant Data Visualisations Using the Grammar of Graphics
googlewayAccesses Google Maps APIs to Retrieve Data and Plot Maps
ggspatialSpatial Data Framework for ggplot2
leafletCreate Interactive Web Maps with Leaflet
mapviewInteractive Viewing of Spatial Data in R
plotlyCreate Interactive Web Graphics via ‘plotly.js’
rasterVisVisualization Methods for Raster Data
tmapThematic Maps

Table 8.1 shows a range of mapping packages are available, and there are many others not listed in this table.Of note is cartography, which generates a range of unusual maps including choropleth, ‘proportional symbol’ and ‘flow’ maps, each of which is documented in the vignette cartography.

Several packages focus on specific map types, as illustrated in Table 8.2.Such packages create cartograms that distort geographical space, create line maps, transform polygons into regular or hexagonal grids, and visualize complex data on grids representing geographic topologies.

Table 8.2: Selected specific-purpose mapping packages, with associated metrics.
PackageTitle
cartogramCreate Cartograms with R
geogridTurn Geospatial Polygons into Regular or Hexagonal Grids
geofacet‘ggplot2’ Faceting Utilities for Geographical Data
globePlot 2D and 3D Views of the Earth, Including Major Coastline
linemapLine Maps

All of the aforementioned packages, however, have different approaches for data preparation and map creation.In the next paragraph, we focus solely on the cartogram package.Therefore, we suggest to read the linemap, geogrid and geofacet documentations to learn more about them.

A cartogram is a map in which the geometry is proportionately distorted to represent a mapping variable.Creation of this type of map is possible in R with cartogram, which allows for creating continuous and non-contiguous area cartograms.It is not a mapping package per se, but it allows for construction of distorted spatial objects that could be plotted using any generic mapping package.

The cartogram_cont() function creates continuous area cartograms.It accepts an sf object and name of the variable (column) as inputs.Additionally, it is possible to modify the intermax argument - maximum number of iterations for the cartogram transformation.For example, we could represent median income in New Zeleand’s regions as a continuous cartogram (the right-hand panel of Figure 8.27) as follows:

  1. library(cartogram)
  2. nz_carto = cartogram_cont(nz, "Median_income", itermax = 5)
  3. tm_shape(nz_carto) + tm_polygons("Median_income")

Comparison of standard map (left) and continuous area cartogram (right).
Figure 8.27: Comparison of standard map (left) and continuous area cartogram (right).

cartogram also offers creation of non-contiguous area cartograms using cartogram_ncont() and Dorling cartograms using cartogram_dorling().Non-contiguous area cartograms are created by scaling down each region based on the provided weighting variable.Dorling cartograms consist of circles with their area proportional to the weighting variable.The code chunk below demonstrates creation of non-contiguous area and Dorling cartograms of US states’ population (Figure 8.28):

  1. us_states2163 = st_transform(us_states, 2163)
  2. us_states2163_ncont = cartogram_ncont(us_states2163, "total_pop_15")
  3. us_states2163_dorling = cartogram_dorling(us_states2163, "total_pop_15")

Comparison of non-continuous area cartogram (left) and Dorling cartogram (right).
Figure 8.28: Comparison of non-continuous area cartogram (left) and Dorling cartogram (right).

New mapping packages are emerging all the time.In 2018 alone, a number of mapping packages have been released on CRAN, includingmapdeck, mapsapi, and rayshader.In terms of interactive mapping, leaflet.extras contains many functions for extending the functionality of leaflet (see the end of the point-pattern vignette in the geocompkg website for examples of heatmaps created by leaflet.extras).