11  Map layout

11.1 Positions

11.2 Layout

11.2.1 Colors

11.2.2 Frame

11.2.3 Margins

11.2.4 Typography

The decision about the used fonts is often neglected when creating programmable plots and maps. Most often, the default fonts are used in these kinds of graphs. This, however, could be a missed opportunity. A lot of map information is expressed by text, including text labels (Section 6.4), legend labels, text in attribute layers (Chapter 10), or the map title (?sec-layout-elements). The used fonts impact the tone of the map (Guidero 2017), and their customization allows for a map to stand out from maps using default options.

As we mentioned above, many different map elements can be expressed or can use fonts. In theory, we are able to set different fonts to each of them. However, this could result in a confusing visual mix that would hinder our map information. Therefore, the decision on the used fonts should be taken after considering the main map message, expected map audience, other related graph styles, etc. In the following three sections, we explain font families and font faces, and give some overall tips on font selections, show how to define used fonts, and how to customize fonts on maps.

11.2.4.1 Font families and faces

(a) Font families
(b) Font faces
Figure 11.1: Basic font families, and font faces implemented in the tmap package.

In tmap, fonts are represented by a font family (Figure 11.1 (a)) and a font face (Figure 11.1 (b)). A font family is a collection of closely related lettering designs. Examples of font families include Times, Helvetica, Courier, Palatino, etc. On the other hand, font faces, such as italic or bold, influence the orientation or width of the fonts. A font is, thus, a combination of a selected font family and font face.

There are a few general font families, such as serifs, sans serifs, and monospaced fonts. Fonts from the serif family have small lines (known as a serif) attached to the end of some letters. Notice, for example, short horizontal lines on the bottom of letters r, i, and f or vertical lines at the ends of the letter s in the top row of Figure 11.1 (a). The fonts in this family are often viewed as more formal. On the other hand, the sans serif family do not have serifs and is considered more informal and modern. The last font family, monospaced fonts, is often used in computer programming (IDEs, software text editors), but less often on maps. A distinguishing feature of the monospaced fonts is that each letter or character in this family has the same width. Therefore, letters, such as i and a will occupy the same space in the monospaced fonts.

Mixing the use of serif and sans serif fonts often works well for maps. However, a rule of thumb is not to mix more than two font families on one map. A sans serif font can be used to label cultural objects, while serif fonts to label physical features. Then, italics, for example, can be used to distinguish water areas. The role of bold font faces, together with increased font size, is to highlight the hierarchy of labels – larger, bold fonts indicate more prominent map features. Additionally, customizing the fonts’ colors can be helpful to distinguish different groups of map objects.

The decision on which fonts to use should also relates to the expected map look and feel. Each font family has a distinct personality (creates a “semantic effect”), which can affect how the map is perceived. Some fonts are more formal, some are less. Some fonts have a modern look, while others look more traditional. Another important concern is personal taste or map branding. We should filter the decision about the used fonts based on our preferences or even our sense of beauty as it could create more personal and unique maps. We just need to remember about the readability of the fonts – they should not be too elaborate as it can hinder the main map message.

11.2.4.2 Fonts available in tmap

Before we discuss how to set a font family and its face, it is important to highlight that a different set of fonts could exist for each operating system (and even each computer). Additionally, which fonts are available and how they are supported depends on the used graphic device. A graphic device is a place where a plot or map is rendered. The most commonly it is a some kind of a screen device, where we can see our plot or map directly after running the R code. Other graphic devices allow for saving plots or maps as files in various formats (e.g., .png, .jpg, .pdf). Therefore, it is possible to get different fonts on your map on the screen, and a (slightly) different one when saved to a file. Visit ?Devices or read the Graphic Devices chapter of Peng (2016) to learn more about graphic devices.

The tmap package has two mechanism to select a font family. The first one is by specifying on of three general font families: "serif", "sans", or "monospace". It tries to match selected general font family with a font family existing on the operating system. For example, "serif" could the "Times" font family, "sans""Helvetica" or "Arial", and "monospace""Courier" (Figure 11.1 (a)). The second mechanism allows to select a font family based on its name (e.g., "Times" or "Palatino"). Next, a member of the selected font families can be selected with one of the font faces: "plain", "italic", "bold", and "bold.italic" (Figure 11.1 (b)).

As mentioned before, available fonts depend on the computer setup (including operating system) and used graphic device. Fonts available on the operating system can be checked with the system_fonts() function of the systemfonts package (Pedersen, Ooms, and Govett 2021) (result not shown).

Information on installing and debugging custom fonts can be found in a blog post by June Choe and in the showtext package (Qiu and See file AUTHORS for details. 2021) documentation.

The next step is to either view or save the map. This also means that we need to carry over our fonts to the output window/file, which largely depends on the selected graphic device. In general, screen device or graphical raster output formats, such as PNG, JPEG, or TIFF, works well with custom fonts as they rasterize them during saving. In case of any problems with graphical raster outputs, it is possible to try alternative graphics devices implemented in the ragg package (Pedersen and Shemanarev 2021). On the other hand, graphical vector formats, such as PDF or SVG, could have some problems with saving maps containing custom fonts1. The PDF device in R, by default, adds metadata about the used fonts, but does not store them. When the PDF reader shows the document, it tries to locate the font on your computer, and use other fonts when the expected one does not exist. An alternative approach is called embedding, which adds a copy of each necessary font to the PDF file itself. Gladly, the creation of a PDF with proper fonts can be achieved in a few ways. Firstly, it could be worth trying some alternative graphic devices such as cairo_pdf or svglite::svglite. The second option is to use the showtext package (Qiu and See file AUTHORS for details. 2021), which converts text into color-filled polygonal outlines for graphical vector formats. Thirdly, the extrafont (Chang 2014) package allows embedding the fonts in the PDF file, which makes PDFs properly displayed on computers that do not have the given font.

11.2.4.3 Fonts on maps

library(tmap)
library(sf)
ei_borders = read_sf("data/easter_island/ei_border.gpkg")
ei_points = read_sf("data/easter_island/ei_points.gpkg")
volcanos = subset(ei_points, type == "volcano")

By default, tmap uses the "sans" font family with the "plain" font face (Figure 11.1). There are, however, three ways to customize the used fonts. The first one is to change all of the fonts and font faces for the whole map at once (Figure 11.2 (a)). This can be done with the text.fontfamily and text.fontface arguments of tm_layout().

tm_shape(ei_borders) +
  tm_polygons() +
  tm_shape(volcanos) +
  tm_text(text = "name", size = "elevation") +
  tm_credits("Data source: OSM") +
  tm_title("Volcanos of Easter Island") +
  tm_layout(text.fontface = "italic",
            text.fontfamily = "serif")

The second way is to specify just some text elements independently (Figure 11.2 (b)). Many tmap functions, such as tm_text() or tm_credits(), have their own fontfamily and fontface arguments that can be adjusted. Additionally, tm_layout() allows to customize fonts for other map elements using prefixed arguments, such as, title.fontface or legend.title.fontfamily.

tm_shape(ei_borders) +
  tm_polygons() +
  tm_shape(volcanos) +
  tm_text(text = "name", size = "elevation", fontfamily = "sans") +
  tm_credits("Data source: OSM", fontface = "bold") +
  tm_title("Volcanos of Easter Island") +
  tm_layout(title.fontface = "bold.italic", 
            legend.title.fontfamily = "monospace")
(a) One font for all elements
(b) Different fonts for different elements
Figure 11.2: Examples of one font (font family and font face) used for all of the map elements (title, text labels, legend, and text annotation), and different fonts used for different map elements.

  1. You can get the invalid font type error when saving the file.↩︎