10. Mapping With Style

Turning up the style

Published

January 1, 2021

Werner Heisenberg is driving down the highway and a police officer stops him. “Sir, do you know you’re going 82 m.p.h.?” the officer asks. “Thanks a lot!” Heisenberg snaps. “Now I’m lost.”

— Unknown

Science flies you to the moon. Religion flies you into buildings.

— Victor Stenger

Now that you have learned the basics of creating a beautiful map in ggplot2 it is time to look at some of the more particular things you will need to make your maps extra stylish. There are also a few more things you need to learn how to do before your maps can be truly publication quality.

If we have not yet loaded the tidyverse let us do so.

# Load libraries
library(tidyverse)
library(scales)
library(ggspatial)

# Load Africa map
load(here::here("data", "africa_map.RData"))

1 Default maps

In order to access the default maps included with the tidyverse we will use the function borders().

ggplot() +
  borders(col = "black", fill = "cornsilk", size = 0.2) + # The global shape file
  coord_equal() # Equal sizing for lon/lat

The built in global shape file.

The built in global shape file.

Jikes! It is as simple as that to load a map of the whole planet. Usually you are not going to want to make a map of the entire planet, so let us see how to focus on just the area around South Africa.

sa_1 <- ggplot() +
  borders(size = 0.2, fill = "cornsilk", colour = "black") +
  coord_equal(xlim = c(12, 36), ylim = c(-38, -22), expand = 0) # Force lon/lat extent
sa_1

A better way to get the map of South Africa.

A better way to get the map of South Africa.

That is a very tidy looking map of South(ern) Africa without needing to load any files.

2 Specific labels

A map is almost always going to need some labels and other visual cues. You saw in the previous section how to add site labels. The following code chunk shows how this differs if yoou want to add just one label at a time. This can be useful if each label needs to be different from all other labels for whatever reason. You may also see that the text labels we are creating have \n in them. When R sees these two characters together like this it reads this as an instruction to return down a line. Let us run the code to make sure you see what this means.

sa_2 <- sa_1 +
  annotate(
    "text",
    label = "Atlantic\nOcean",
    x = 15.1,
    y = -32.0,
    size = 5.0,
    angle = 30,
    colour = "navy"
  ) +
  annotate(
    "text",
    label = "Indian\nOcean",
    x = 33.2,
    y = -34.2,
    size = 5.0,
    angle = 330,
    colour = "red4"
  )
sa_2

Map of southern Africa with specific labels.

Map of southern Africa with specific labels.

3 Scale bars

With your fancy labels added, let us insert a scale bar next. There is no default scale bar function in the tidyverse, which is why you have loaded the ggsn package. This package is devoted to adding scale bars and North arrows to ggplot2 figures. There are heaps of options so you will just focus on one of them for now. It is a bit finicky so to get it looking exactly how you want it requires some guessing and checking. Please feel free to play around with the coordinates below. You may see the list of available North arrow shapes by running northSymbols().

sa_3 <- sa_2 +
  annotation_scale(
    location = "bl", # bottom left; you can also specify "tl", "tr", etc.
    width_hint = 0.2, # relative width of the scale bar
    height = unit(0.3, "cm"), # scale bar height
    text_cex = 0.8, # text size
    pad_x = unit(0.5, "cm"), # horizontal offset
    pad_y = unit(0.5, "cm"), # vertical offset
    bar_cols = c("black", "white")
  ) +
  annotation_north_arrow(
    location = "bl",
    which_north = "true", # true north, not grid north
    pad_x = unit(3, "cm"), # adjust to approximate your original x.min/x.max
    pad_y = unit(2, "cm"), # adjust to approximate your original y.min/y.max
    style = north_arrow_fancy_orienteering
  )

sa_3

Map of southern Africa with labels and a scale bar.

Map of southern Africa with labels and a scale bar.

Reuse

Citation

BibTeX citation:
@online{smit,_a._j.2021,
  author = {Smit, A. J.,},
  title = {10. {Mapping} {With} {Style}},
  date = {2021-01-01},
  url = {http://tangledbank.netlify.app/BCB744/intro_r/10-mapping_style.html},
  langid = {en}
}
For attribution, please cite this work as:
Smit, A. J. (2021) 10. Mapping With Style. http://tangledbank.netlify.app/BCB744/intro_r/10-mapping_style.html.