8 Making maps with R
- This chapter requires the following packages that we have already been using:
- In addition it uses the following visualization packages:
A satisfying and important aspect of geographic research is producing and communicating the results in the form of maps.
Map making — the art of Cartography — is an ancient skill that involves precision, consideration of the map-reader and often an element of creativity.
Basic plotting of geographic data is straightforward in R with
plot() (see section 2.1.3), and it is possible to create advanced maps using base R methods (Murrell 2016).
This chapter focuses on dedicated map-making packages, especially tmap, for reasons that are explained in section 8.2. There are a multitude of options but when learning a new skill (in this case map making), it makes sense to gain depth-of-knowledge in one package before branching out, a concept that also applies to the choice of programming language as we saw in Chapter 1. It is worth developing advanced map making skills not only because it is a fun activity that can produce beautiful results. Map making also has important practical applications. A carefully crafted map can help ensure that time spent in the analysis phases of geocomputational projects — for example using methods covered in chapters 2 to 6 — are communicated effectively (Brewer 2015):
Amateur-looking maps can undermine your audience’s ability to understand important information and weaken the presentation of a professional data investigation.
Maps have been used for several thousand years for a wide variety of purposes. Historic examples include maps of buildings and land ownership in the Old Babylonian dynasty more than 3000 years ago and Ptolemy’s world map in his masterpiece Geography nearly 2000 years ago (Talbert 2014). However, maps have historically been out of reach for everyday people.
Modern computing has the potential to change this. Map making skills can also help meet research and public engagement objectives. From a research perspective clear maps are often the best way to present the results of geocomputational research. From policy and ‘citizen science’ perspectives, attractive and engaging maps can help change peoples’ minds, based on the evidence. Map making is therefore a critical part of geocomputation and its emphasis not only on describing, but also changing the world (see Chapter 1).
8.2 Static maps
Static maps are the most common type of visual output from geocomputation.
They are fixed images that can be included in printed outputs or published online.
The majority of maps contained in this book are static maps saved as
.png files (interactive maps are covered in section 8.4).
plot() function is often the fastest way to create static maps from vector and raster spatial objects, as demonstrated in sections 2.1.3 and 2.2.2.
Sometimes the simplicity and speed of this approach is sufficient, especially during the development phase of a project:
when using R interactively to understand a geographic dataset, you will likely be the only person who sees them.
The base R approach is also extensible, with
plot() offering dozens of arguments and the grid providing functions for low-level control of graphical outputs, — see R Graphics (Murrell 2016), especially Chapter 14.
The focus of this section, however, is making static maps with tmap.
It is a powerful and flexible map-making package with sensible defaults.
It has a concise syntax that allows for the creation of attractive maps with minimal code, that will be familiar to ggplot2 users.
Furthermore, tmap has a unique capability to generate static and interactive maps using the same code via
It accepts a wider range of spatial classes (including
raster objects) than alternatives such as ggplot2, as documented in vignettes
tmap-modes and an academic paper on the subject (Tennekes 2018).
This section teaches how to make static maps with tmap, emphasizing the important aesthetic and layout options.
8.2.1 tmap basics
Like ggplot2, tmap is based on the idea of a ‘grammar of graphics’ (Wilkinson and Wills 2005).
This involves a separation between the input data and the aesthetics (how data are visualised): each input dataset can be ‘mapped’ in a range of different ways including location on the map (defined by data’s
geometry), color, and other visual variables.
The basic building block is
tm_shape() (which defines input data, raster and vector objects), followed by one or more layer elements such as
This layering is demonstrated in the chunk below, which generates the maps presented in Figure 8.1:
The object passed to
tm_shape() in this case is
sf object representing the regions of New Zealand (see section 2.1.1 for more on
Layers are added to represent
nz visually, with
tm_borders() creating shaded areas (left panel) and border outlines (middle panel) in Figure 8.1, respectively.
This is an intuitive approach to map making:
the common task of adding new layers is undertaken by the addition operator
+, followed by
The asterisk (*) refers to a wide range of layer types which have self-explanatory names including
borders (demonstrated above),
(see the help page
tmap-element for a full list).
This layering is illustrated in the right panel of Figure 8.1, the result of adding a border on top of the fill layer:
tm_borders() call after
tm_fill() + in the previous code chunk.
qtm()is a handy function for quickly creating tmap maps (hence the snappy name). It is concise and provides a good default visualization in many cases:
qtm(nz), for example, is equivalent to
tm_shape(nz) + tm_fill() + tm_borders(). Further, layers can be added concisely using multiple
qtm()calls, such as
qtm(nz) + qtm(nz_height). The disadvantage is that it makes aesthetics of individual layers harder to control, explaining why we avoid teaching it in this chapter.
8.2.2 Map objects
A useful feature of tmap is its ability to store objects representing maps.
The code chunk below demonstrates this by saving the last plot in Figure 8.1 as an object of class
tmap (note the use of
tm_polygons() which condenses
tm_fill() + tm_borders() into a single function):
map_nz can be plotted later, for example by adding additional layers (as shown below) or simply running
map_nz in the console, which is equivalent to
New shapes can be added with
In this case
new_obj represents a new spatial object to be plotted on top of preceding layers.
When a new shape is added in this way all subsequent aesthetic functions refer to it, until another new shape is added.
This syntax allows the creation of maps with multiple shapes and layers, as illustrated in the next code chunk which uses the function
tm_raster() to plot a raster layer (with
alpha set to make the layer semi-transparent):
Building on the previously created
map_nz object, the preceding code creates a new map object
map_nz1 that contains another shape (
nz_eleve) representing average elevation across New Zealand (see Figure 8.2, left).
More shapes and layers can be added, as illustrated in the code chunk below which creates
nz_water, representing New Zealand’s territorial waters, and adds the resulting lines to an existing map object.
There is no limit to the number of layers or shapes that can be added to
The same shape can even be used multiple times.
The final map illustrated in Figure 8.2 is created by adding a layer representing high points (stored in the object
nz_height) onto the previously created
map_nz2 object with
?tm_bubbles for details on tmap’s point plotting functions).
The resulting map, which has four layers, is illustrated in the right-hand panel of:
A useful and little known feature of tmap is that multiple map objects can be arranged in a single ‘metaplot’ with
This is demonstrated in the code chunk below which plots
map_nz3, resulting in Figure 8.2.
Additional elements can also be added with the
Aesthetic settings, however, are controlled by arguments to layer functions.
The plots in the previous section demonstrate tmap’s default aesthetic settings.
Grey shades are used for
tm_bubbles() layers and a continuous black line is used to represent lines created with
Of course, these default values and other aesthetics can be overridden.
The purpose of this section is to show how.
There are two main types of map aesthetics: those that change with the data and those that are constant.
Unlike ggplot2, which uses the helper function
aes() to represent variable aesthetics, tmap accepts aesthetic arguments that are either variable fields (based on column names) or constant values.34
The most commonly used aesthetics for fill and border layers include color, transparency, line width and line type, set with
lty arguments respectively.
The impact of setting these with fixed values is illustrated in Figure 8.3.
ma1 = tm_shape(nz) + tm_fill(col = "red") ma2 = tm_shape(nz) + tm_fill(col = "red", alpha = 0.3) ma3 = tm_shape(nz) + tm_borders(col = "blue") ma4 = tm_shape(nz) + tm_borders(lwd = 3) ma5 = tm_shape(nz) + tm_borders(lty = 2) ma6 = tm_shape(nz) + tm_fill(col = "red", alpha = 0.3) + tm_borders(col = "blue", lwd = 3, lty = 2) tmap_arrange(ma1, ma2, ma3, ma4, ma5, ma6)
Like base R plots, arguments defining aesthetics can also receive values that vary. Unlike the base R code below (which generates the left panel in Figure 8.4), tmap aesthetic arguments will not accept a numeric vector:
col (and other aesthetics that can vary such as
lwd for line layers and
size for point layers) requires a character string naming an attribute associated with the geometry to be plotted.
Thus, one would achieve the desired result as follows (plotted in the right-hand panel of Figure 8.4):
An important argument in functions defining aesthetic layers such as
title, which sets the title of the associated legend.
The following code chunk demonstrates this functionality by providing a more attractive name than the variable name
Land_area (note the use of
expression() to create superscript text):
8.2.4 Color settings
Color settings are an important part of map design. They can have a major impact on how spatial variability is portrayed as illustrated in Figure 8.5. This shows four ways of coloring regions in New Zealand depending on median income, from left to right (and demonstrated in the code chunk below):
- The default setting uses ‘pretty’ breaks, described in the next paragraph
breakswhich allows you to manually set the breaks
nwhich sets the number of bins into which numeric variables are categorized
palettewhich defines the color scheme, for example
Another way to change color settings is by altering color break (or bin) settings.
In addition to manually setting
breaks tmap allows users to specify algorithms to automatically create breaks with the
Six of the most useful break styles are illustrated in Figure 8.6 and described in the bullet points below:
style = pretty, the default setting, rounds breaks into whole numbers where possible and spaces them evenly
style = equaldivides input values into bins of equal range, and is appropriate for variables with a uniform distribution (not recommended for variables with a skewed distribution as the resulting map may end-up having little color diversity)
style = quantileensures the same number of observations fall into each category (with the potential down side that bin ranges can vary widely)
style = jenksidentifies groups of similar values in the data and maximizes the differences between categories
style = cont(and
order) present a large number of colors over continuous color field, and are particularly suited for continuous rasters (
ordercan help visualize skewed distributions)
style = catwas designed to represent categorical values and assures that each category receives a unique color
styleis an argument of tmap functions it in fact originates as an argument in
classInt::classIntervals()— see the help page of this function for details.
Palettes define the color ranges associated with the bins and determined by the
style arguments described above.
The default color palette is specified in
tm_layout() (see section 8.2.5 to learn more), however, it could be quickly changed using the
It expects a vector of colors or a new color palette name, which can be selected interactively with
You can add a
- as prefix to reverse the palette order.
There are three main groups of color palettes - categorical, sequential and diverging (Figure 8.7), and each of them serves a different purpose. Categorical palettes consist of easily distinguishable colors and are most appropriate for categorical data without any particular order such as state names or land cover classes. Colors should be intuitive: rivers should be blue, for example, and pastures green. Avoid too many categories: maps with large legends and many colors can be uninterpretable.35
The second group is sequential palettes.
These follow a gradient, for example from light to dark colors (light colors tend to represent lower values), and are appropriate for continuous (numeric) variables.
Sequential palettes can be single (
Blues go from light to dark blue for example) or multi-color/hue (
YlOrBr is gradient from light yellow to brown via orange, for example), as demonstrated in the code chunk below — output not shown, run the code yourself to see the results!
The last group, diverging palettes, typically range between three distinct colors (purple-white-green in Figure 8.7) and are usually created by joining two single color sequential palettes with the darker colors at each end.
Their main purpose is to visualize the difference from an important reference point, e.g. a certain temperature, the median household income or the mean probability for a drought event.
The reference point’s value can be adjusted in tmap using the
There are two important principles for consideration when working with colors - perceptibility and accessibility. Firstly, colors on maps should match our perception. This means that certain colors are viewed through our experience and also cultural lenses. For example, green colors usually represent vegetation or lowlands and blue is connected with water or cool. Color palettes should also be easy to understand to effectively convey information. It should be clear which values are lower and which are higher, and colors should change gradually. This property is not preserved in the rainbow color palette, therefore we suggest avoiding it in spatial data visualization (Borland and Taylor II 2007). Instead the viridis color palettes, also available in tmap, can be used. Secondly, changes in colors should be accessible to the largest number of people. Therefore, it is important to use colorblind friendly palettes as often as possible.36
The map layout refers to the combination of all map elements into a cohesive map. Map elements include among others the objects to be mapped, the title, the scale bar, margins and aspect ratios, while the color settings covered in the previous section relate to the palette and break-points used to affect how the map looks. Both may result in subtle changes that can have an equally large impact on the impression left by your maps.
Additional elements such as north arrows and scale bars have their own functions -
tm_scale_bar() (Figure 8.8).
tmap also allows a wide variety of layout settings to be changed, some of which are illustrated in Figure 8.9, produced using the following code (see
?tm_layout for a full list):
The other arguments in
tm_layout() provide control over many more aspects of the map in relation to the canvas on which it is placed.
Some useful layout settings are listed below (see Figure 8.10 for illustrations of a selection of these):
- Frame width (
frame.lwd) and an option to allow double lines (
- Margin settings including
- Font settings, controlled by
- Legend settings including binary options such as
legend.show(whether or not to show the legend)
legend.only(omit the map) and
legend.outside(should the legend go outside the map?), as well as multiple choice settings such as
- Default colors of aesthetic layers (
aes.color), map attributes such as the frame (
- Color settings controlling
sepia.intensity(how yellowy the map looks) and
The impact of changing the color settings listed above is illustrated in Figure 8.11 (see
?tm_layout for a full list).
Beyond the low-level control over layouts and colors, tmap also offers high-level styles, using the
tm_style() function (representing the second meaning of ‘style’ in the package).
Some styles such as
tm_style("cobalt") result in stylized maps while others such as
tm_style("grey") make more subtle changes, as illustrated in Figure 8.12, created using code below (see
tmap_style_catalogue(). This creates a folder called
tmap_style_previewscontaining nine images. Each image, from
tm_style_white.pngshows a faceted map of the world in the corresponding style. Note:
tmap_style_catalogue()takes some time to run.
8.2.6 Faceted maps
Faceted maps, also referred to as ‘small multiples’, are composed of many maps arranged side-by-side, and sometimes stacked vertically. Facets enable the visualization of how spatial relationships change with respect to another variable, such as time. The changing populations of settlements, for example, can be represented in a faceted map with each panel representing the population at a particular moment in time. The time dimension could be represented via another aesthetic such as color. However, this risks cluttering the map because it will involve multiple overlapping points (cities do not tend to move over time!).
Typically all individual facets in a faceted map contain the same geometry data repeated multiple times, once for each column in the attribute data (this is the default plotting method for
sf objects, see 2).
However, facets can also represent shifting geometries such as as the evolution of a point pattern over time.
This use case of faceted plot is illustrated in Figure 8.13.
The preceding code chunk demonstrates key features of faceted maps created with tmap:
- Shapes that do not have a facet variable are repeated (the countries in
worldin this case).
byargument which varies depending on a variable (
yearin this case).
ncolsetting specifying the number of rows and columns that facets should be arranged into.
free.coords-parameter specifying if each map has its own bounding box.
In addition to their utility for showing changing spatial relationships, faceted maps are also useful as the foundation for animated maps (see 8.3).
8.2.7 Inset maps
An inset map is a smaller map rendered within or next to the main map. It could serve many different purposes, including providing a context (Figure 8.14) or bringing some non-contiguous regions closer to ease their comparison (Figure 8.15). They could be also used to focus on a smaller area in more detail or to cover the same area as the map but representing a different topic.
In the example below, we create a map of the central part of the New Zealand’s Southern Alps.
Our inset map will show where the main map is in relation to the whole New Zealand.
The first step is to define the area of interest, which can be done by creating a new spatial object,
In the second step, we create a base map showing the New Zealand’s Southern Alps area. This is a place where the most important message is stated.
The third step consists of the inset map creation. It gives a context and helps to locate the area of interest. Importantly, this map needs to clearly indicate the location of the main map, for example by stating its borders.
Finally, we combine the two maps.
A viewport from the grid package can be used by stating a center location (
y) and a size (
height) of the inset map.
Inset map can be save to file either by using a graphic device (see section 7.7) or the
tmap_save() function and its arguments -
Inset maps are also used to create one map of non-contiguous areas.
Probably, the most often used example is a map of United States, which consists of the contiguous United States, Hawaii and Alaska.
It is very important to find the best projection for each individual inset in these type of cases (see section 6 to learn more).
We can use US National Atlas Equal Area for the map of the contiguous United States by putting its EPSG code in the
projection argument of
The rest of our objects,
alaska, already have proper projections, therefore we just need to create two separate maps:
The final map is created by combining and arranging these three maps:
The code presented above is very compact and allows for creation of many similar maps, however the map do not represent sizes and locations of Hawaii and Alaska well.
You can see an alternative approach in the
vignettes/us-map.Rmd file in the book’s GitHub repo, which tries to mitigate these issues.
The main goal of this section is to present how to generate and arrange inset maps. The next step is to use the knowledge from the previous sections to improve the map style or to add another data layers. Moreover, the same skills can be applied to combine maps and plots.
8.3 Animated maps
Faceted maps, described in 8.2.6, provide a way of showing how spatial relationships vary but the approach has disadvantages. Facets become tiny when many of them are squeezed into a single plot, potentially hiding any spatial relationships. Furthermore the fact that each facet is separated by space on the screen or page means that subtle differences between facets can be hard to detect.
With an increasing proportion of communication happening via digital screens, animated maps are becoming more popular. Animated maps can even be useful for paper reports: you can always link readers to a web-page containing an animated (or interactive) version of a printed map to help make it come alive.
Figure 8.16 is a simple example of an animated map. Unlike the faceted plot it does not squeeze multiple maps into a single screen and allows the reader to see how the spatial distribution of the worlds most populous agglomerations evolve over time (see the book’s website for the animated version).
The animated map illustrated in Figure 8.16 can be created using the same tmap techniques that generates faceted maps, demonstrated in section 8.2.6.
There are two differences, however, related to arguments in
free.coords = FALSE, which maintains the map extent for each map iteration.
nrow = 1and
ncol = 1ensure only one facet is created per year.
These additional arguments are demonstrated in the subsequent code chunk:
urb_anim represents a set of separate maps for each year.
The final stage is to combine them and save the result as a
.gif file with
The following command creates the animation illustrated in Figure 8.16, with a few elements missing, that we will add-in during the exercises:
Another illustration of the power of animated maps is provided in Figure 8.17.
This shows the development of states in United States, which first formed in the East and then incrementally to the West and finally into the interior.
Code to reproduce this map can be found in the script
8.4 Interactive maps
Static and animated maps can breathe life into geographic datasets. Interactive maps can take reader participation to a whole new level, providing results with a life of their own that can be explored in myriad ways. Interactivity can take many forms, including the appearance of popup messages when users click or mouse-over geographic features and maps dynamically linked to non-geographic plots.37
The most important type of interactivity, however, is the display of geographic data on an interactive or ‘slippy’ web maps. The release of the leaflet package in 2015 revolutionized interactive web map creation from within R and a number of packages have built on these foundations adding new features (e.g. leaflet.extras) and making the creation of web maps as simple as creating static maps (e.g. mapview and tmap). This section illustrates each approach in the opposite order. We will explore how to make slippy maps with tmap (the syntax of which we have already learned), mapview and finally leaflet (which provides low level control over interactive maps).
A unique feature of tmap mentioned in section 8.2 is its ability to create static and interactive maps using the same code.
Maps can be viewed interactively at any point by switching to view mode, using the command
This is demonstrated in the code below, which creates an interactive map of New Zealand based on the
map_nz, created in section 8.2.2, and illustrated in Figure 8.18:
#> tmap mode set to interactive viewing
Now that the interactive mode has been ‘turned on’, all maps produced with tmap will launch (another way to create interactive maps is with the
tmap_leaflet function) .
Notable features of this interactive mode include the ability to specify the basemap using the
basemaps argument in the function
tm_view() (also see
?tm_basemap), as demonstrated below (the result, a map of New Zealand with an interactive topographic basemap, is not shown):
An impressive and little-known feature of tmap’s view mode is that it also works with faceted plots.
tm_facets() can be used in this case to produce multiple maps with synchronized zoom and pan settings, as illustrated in Figure 8.19 which was produced by the following code:
Switch tmap back to plotting mode with the same function:
If you are not proficient with tmap, the quickest way to create an interactive maps may be with mapview. The following ‘one liner’ is a reliable way to interactively explore a wide range of spatial data formats:
mapview has a concise syntax yet is powerful. By default, it provides some standard GIS functionality such as mouse position information, attribute queries (via pop-ups), scale bar, zoom-to-layer buttons.
It offers advanced controls including the ability to ‘burst’ datasets into multiple layers and the addition of multiple layers with
+ followed by the name of a geographic object. Additionlaly, it provides automatic coloring of attributes (via argument
zcol). In essence, it can be considered a data-driven leaflet API (see below for more information about leaflet). Given that mapview always expects a spatial object (sf, sp, raster) as its first argument, it works well at the end of piped expressions. Consider the following example where sf is used to intersect lines and polygons and then visualised with mapview.
One important thing to keep in mind is that mapview layers are added via the
+ operator (similar to ggplot2). This is a frequent gotcha in piped workflows where the main binding operator is
For further information on mapview see the package’s website at r-spatial.github.io/mapview/.
There are other ways to create interactive maps with R not demonstrated here due to space constraints.
The googleway package, for example, provides an interactive mapping interface that is flexible and extensible with
google_map(key = key) %>% add_polygons(st_transform(nz, 4326)) plots an interactive map of New Zealand (it assumes a Google API key is saved as
Many other functions are provided by the package, providing an R interface to a wide range of mapping services including routing, traffic visualization and geocoding (see the
googleway-vignette for details).
Leaflet maps are created with
leaflet(), the result of which is a
leaflet map object which can be piped to other leaflet functions.
This allows multiple map layers and control settings to be added interactively, as demonstrated in the code below which generates Figure 8.22 (see rstudio.github.io/leaflet/ for details).
pal = colorNumeric("RdYlBu", domain = cycle_hire$nbikes) leaflet(data = cycle_hire) %>% addProviderTiles(providers$Stamen.TonerLite) %>% addCircles(col = ~pal(nbikes), opacity = 0.9) %>% addPolygons(data = lnd, fill = FALSE) %>% addLegend(pal = pal, values = ~nbikes) %>% setView(lng = -0.1, 51.5, zoom = 12) %>% addMiniMap()