Skip to contents

Recall from the vignette about components that the position argument of map components specifies their position, and that components can be grouped. This article explains both in detail: the positioning of components (first part) and the grouping of components (second part).

It can be specified in two ways:

We will use a self-made map component, made with the grid package that we show via tm_inset().

g = function(text) grid::gList(
  grid::rectGrob(gp=grid::gpar(fill = "gold")), 
  grid::circleGrob(r = .45), 
  grid::textGrob(text))
b = function(text) grid::gList(
  grid::rectGrob(gp=grid::gpar(fill = "steelblue")), 
  grid::circleGrob(r = .45), 
  grid::textGrob(text))

Shortcut method (inside the map frame)

The position can be a vector of two values, where the first is the horizontal position and the second the vertical position. It is a shortcut for tm_pos_in(), which is explained in the next sections.

There are two options:

Character vector of two

  • the first value specifies the horizontal position ("left", "center", "right"), and
  • the second one the vertical position ("top", "center", "bottom").

Upper case means tight to the frame. Otherwise an offset margin is applied (can be specified with offset, see below).

Numeric vector of two

This can be seen as the ‘manual’ mode, because it provides more freedom.

  • the first value specifies the horizontal position, where 0 corresponds to left and 1 to right.
  • the second value specifies the vertical position where 0 is bottom and 1 top.

By default, these values will specify the position of the left top corner of the component. This justification point can be changed using the just.h and just.v arguments of tm_pos(), see below.

Example

The character positioned components are in gold and the numeric positioned components in blue.

tm_shape(NLD_muni) +
    tm_polygons("grey85", col = "grey60") +
    tm_inset(g("left, top"), position = c("left", "top")) +
    tm_inset(g("right, bottom"), position = c("right", "bottom")) +
    tm_inset(g("LEFT, BOTTOM"), position = c("LEFT", "BOTTOM")) +
    tm_inset(b("0.3, 0.3"), position = c(0.2, 0.2)) +
    tm_inset(b("0.5, 0.5"), position = c(0.5, 0.5)) +       
    tm_inset(b("0.9, 1"), position = c(0.9, 1))     

Advanced method with tm_pos()

The 3 x 3 grid

The whole plot area is a 3 x 3 grid, where the map (or facets) are drawn in the center grid cell.

There are three variants (wrappers) of the main function tm_pos():

  • tm_pos_out() draws the component outside the map frame (so in one of the 3 x 3 grid cells)
  • tm_pos_in() and tm_pos_on_top() draw the component in the middle grid cell. The difference is that the former takes the frame (and offset from it) into account while the latter doesn’t.

cell.h and cell.v

The arguments cell.h and cell.v determine the grid cell. These should only be used for tm_pos_out(). For the other position function, they are already set to "center" (the center grid cell in which the map is drawn).

tm_shape(NLD_muni) +
    tm_polygons("grey85", col = "grey60") +
tm_inset(g("center, center"), position = tm_pos_on_top(pos.h = "center", pos.v = "center")) +
tm_inset(g("left, center"), position = tm_pos_out("left", "center", pos.v = "center")) +
tm_inset(g("right, center"), position = tm_pos_out("right", "center", pos.v = "center")) +
tm_inset(g("left, top"), position = tm_pos_out("left", "top")) +
tm_inset(g("center, top"), position = tm_pos_out("center", "top", pos.h = "center")) +
tm_inset(g("right, top"), position = tm_pos_out("right", "top")) +
tm_inset(g("left, bottom"), position = tm_pos_out("left", "bottom")) +
tm_inset(g("center, bottom"), position = tm_pos_out("center", "bottom", pos.h = "center")) +
tm_inset(g("right, bottom"), position = tm_pos_out("right", "bottom")) +
tm_layout(asp = 1, frame.color = "blue", frame.lwd = 2)

Note that we used pos.h and pos.v. These will be explained in the next section.

pos.h and pos.v

The position of a component within a cell. These values are similar to the shortcut values described above:

  • Character values: "left", "center", or "right" (or in upper case) for pos.h and "top", "center", and "bottom" for pos.v.
  • Numeric values: 0 (left) to 1 (right) for pos.h, and 0 (bottom) to 1 (top) for pos.v
tm_shape(NLD_muni) +
  tm_polygons("grey85", col = "grey60") +
tm_inset(g("pos.v = \"top\" in\ncell (left, center)"), 
  position = tm_pos_out("left", "center", pos.v = "top")) +
tm_inset(g("pos.v = \"bottom\" in\ncell (right, center)"), 
  position = tm_pos_out("right", "center", pos.v = "bottom")) +
tm_inset(b("pos.h = 0.0\npos.v = 0.0\nin center cell"),
  position = tm_pos_in(pos.h = 0.0, pos.v = 0.0)) +
tm_inset(b("pos.h = 0.5\npos.v = 0.5\nin center cell"),
  position = tm_pos_in(pos.h = 0.5, pos.v = 0.5)) +
tm_inset(b("pos.h = 1\npos.v = 1\nin center cell"),
  position = tm_pos_in(pos.h = 1, pos.v = 1)) +
tm_layout(asp = 1, frame.color = "blue", frame.lwd = 2)

Recall that the justification point for the numeric specifications is the top left corner of the component. This can be adjusted via just.h and just.v as explained next.

just.h and just.v

In case pos.h and pos.v are numbers, the justification can be set with

  • just.h: "left", "center", or "right"
  • just.v: "top", "center", and "bottom"
tm_shape(NLD_muni) +
  tm_polygons("grey85", col = "grey60") +
tm_inset(b("center, center"), 
  position = tm_pos_in(0.5, 0.5, just.h = "center", just.v = "center")) +
tm_inset(b("left, top"), 
  position = tm_pos_in(0.5, 0.5, just.h = "left", just.v = "top")) +
tm_inset(b("right, bottom"), 
  position = tm_pos_in(0.5, 0.5, just.h = "right", just.v = "bottom")) +
tm_layout(asp = 1, frame.color = "blue", frame.lwd = 2)

align.h and align.v

In case there are multiple components in the same position (see next section), the alignment determines how a component is aligned.

The alignment of components within the same cell:

tm_shape(NLD_muni) +
  tm_polygons("grey85", col = "grey60") +
tm_inset(g(""), 
  width = 4, height = 4,
  position = tm_pos_in("left", "top")) +
tm_inset(b("left"),
  width = 3, height = 3,
  position = tm_pos_in("left", "top", align.h = "left")) +
tm_inset(b("right"),
  width = 3, height = 3,
  position = tm_pos_in("left", "top", align.h = "right")) +
tm_inset(g(""), 
  width = 4, height = 4,
  position = tm_pos_in("left", "top")) +
tm_layout(asp = 1, frame.color = "blue", frame.lwd = 2)

Automatic positioning

The default position of legends and map components are in some cases done automatically. Automatic positioning can be achieved by setting a position argument to tm_pos_auto_in() or tm_pos_auto_out(). The former checks in which map corner is most space available (and bases pos.h and pos.v on that). The latter calculates in which grid cell (so cell.h and cell.v) the map components can be positioned, which is based on the aspect ratio and margins.

Grouping components

What happens when components share the same position? They are grouped. Components can also be grouped explicitly via tm_components(). This section explains how they are grouped and what the grouping options are.

Grouping components via the map component functions

In the following map, each map component is placed in a different location.

(tm1 = tm_shape(NLD_muni) +
    tm_bubbles(fill = "dwelling_value", 
               size = "population",
               size.legend = tm_legend(position = tm_pos_out("right", "center", pos.v = "bottom"))) +
    tm_compass(position = c("left", "top")) +
    tm_scalebar() +
    tm_credits("Statistics Netherlands", position = c("left", "bottom")))

Let’s put all legends to the left hand side of the map, and the compass and scale bar below the frame.

tm1 + 
    tm_components("tm_legend", position = tm_pos_out("left", "center")) +
    tm_components(c("tm_compass", "tm_scalebar"), position = tm_pos_out("center", "bottom"))

All map components that are not title, chart, or title can be accessed via tm_<other>:

tm1 + 
    tm_components("tm_legend", position = tm_pos_out("left", "center")) +
    tm_components("tm_<other>", position = tm_pos_out("center", "bottom"))

When tm_component() is called without group_id, all map components are affected:

tm1 + 
    tm_components("tm_legend", position = tm_pos_out("left", "center")) +
    tm_components("tm_<other>", position = tm_pos_out("center", "bottom")) +
    tm_components(position = tm_pos_out("right", "center"))

Note that each time a tm_components() is called, it will update the map. So in this case, the first two tm_components() are overruled with the third one. If we change the order, the map components are placed in different locations:

tm1 + 
    tm_components("tm_<other>", position = tm_pos_out("center", "bottom")) +
    tm_components(position = tm_pos_out("right", "center")) +
    tm_components("tm_legend", position = tm_pos_out("left", "center"))

Now all components are placed to the right hand side of the map (second tm_components() call), except for the legends, which are placed on the left hand side of the map (third tm_components() call).

Grouping components via group_id

Map components can also be grouped via a self-specified group_id, which should also be specified in the map component function (e.g. tm_legend()).

To illustrate this and further explain tm_components(), we will use a self-made map component, made with the grid package that we show via tm_inset().

g = function(text) grid::gList(
  grid::rectGrob(gp=grid::gpar(fill = "gold")), 
  grid::circleGrob(r = .45), 
  grid::textGrob(text))
b = function(text) grid::gList(
  grid::rectGrob(gp=grid::gpar(fill = "steelblue")), 
  grid::circleGrob(r = .45), 
  grid::textGrob(text))
tm2 = tm_shape(NLD_muni) +
    tm_polygons("grey95", col = "grey90") +
tm_inset(g("A1"), group_id = "A") +
tm_inset(g("A2"), group_id = "A") +
tm_inset(b("B1"), group_id = "B", height = 3, width = 3) +
tm_inset(g("A3"), group_id = "A") +
tm_inset(b("B2"), group_id = "B", height = 3, width = 3) +
tm_layout(asp = 1)

tm2 + 
  tm_components("A", position = tm_pos_in("left", "top")) +
  tm_components("B", position = tm_pos_in("right", "bottom"))

Under the hood, the actual grouping is done by position alone: all components with the same position are considered as a group and therefore stacked instead of drawn on top of each other. In this example components of A and B have the same position and are therefore grouped, even though they have a different group_id.

tm2 + 
  tm_components("A", position = tm_pos_in("right", "bottom")) +
  tm_components("B", position = tm_pos_in("right", "bottom"))

Grouping options

Stacking

The goal of tm_components() is not only to group map components, but also to specify group related arguments, as we will show now.

Direction

The stack argument is either "horizontal" or "vertical":

tm2 + 
  tm_components("A", position = tm_pos_in("left", "top"), stack = "horizontal") +
  tm_components("B", position = tm_pos_in("right", "bottom"), stack = "vertical")

Order

The stacking order is by default the same as in which the components appear, but can be changed with the z argument in each component function:

tm_shape(NLD_muni) +
  tm_polygons("grey95", col = "grey90") +
tm_inset(g("A1"), group_id = "A", z = 1) +
tm_inset(g("A2"), group_id = "A", z = 3) +
tm_inset(g("A3"), group_id = "A", z = 2) +
tm_layout(asp = 1) +
tm_components("A", position = tm_pos_in("left", "top")) 

Frame

frame_combine

By default frame_combine = TRUE, which means that a single frame is drawn for all components in the same group. This can be disabled with frame_combine = FALSE

tm_shape(NLD_muni) +
  tm_polygons("grey95", col = "grey90") +
tm_inset(g("A1"), group_id = "A") +
tm_inset(g("A2"), group_id = "A", width = 3, height = 3) +
tm_inset(g("A3"), group_id = "A") +
tm_layout(asp = 1) +
tm_components("A", position = tm_pos_in("left", "top"), frame_combine = FALSE) 

equalize

Equalize (by default TRUE) means that components made equal in size, that is: the widths in case they are stacked vertically (as in this example), and the heights in case they are stacked horizontally.

tm_shape(NLD_muni) +
  tm_polygons("grey95", col = "grey90") +
tm_inset(g("A1"), group_id = "A") +
tm_inset(g("A2"), group_id = "A", width = 3, height = 3) +
tm_inset(g("A3"), group_id = "A") +
tm_layout(asp = 1) +
tm_components("A", position = tm_pos_in("left", "top"), frame_combine = FALSE, equalize = FALSE) 

Margins

General offset and stack_margin

The offset specifies the distance (in line heights) between the frame and components. By default 0.75 in case the components are drawn inside the frame and pos.h and pos.v are lower case, and 0 otherwise.

The stack_margin specifies the distance between components. In case frame_combine it is 0.5 by default, otherwise 0.

tm_shape(NLD_muni) +
  tm_polygons("grey95", col = "grey90") +
tm_inset(g("A1"), group_id = "A") +
tm_inset(g("A2"), group_id = "A", width = 3, height = 3) +
tm_inset(g("A3"), group_id = "A") +
tm_layout(asp = 1) +
tm_components("A", position = tm_pos_in("left", "top"), frame_combine = FALSE, equalize = FALSE, offset = 0, stack_margin = 1)

Note: currently both offset and stack_margin are applied for all components for which pos.h and pos.v are specified as characters.

Component specific margins

Each component has its own margins, which are the margins to the frame. These can be specified with a vector of four: bottom, left, top, right.

tm_shape(NLD_muni) +
  tm_polygons("grey95", col = "grey90") +
tm_inset(g("A1"), group_id = "A", margins = 0) +
tm_inset(g("A2"), group_id = "A", width = 3, height = 3, margins = 0) +
tm_inset(g("A3"), group_id = "A", margins = 0) +
tm_layout(asp = 1) +
tm_components("A", position = tm_pos_in("left", "top"), frame_combine = T, frame = TRUE, offset = 0, stack_margin = 0)

Alignment

Recall that the align.h and align.v arguments of tm_pos() specify the alignment of the components (see align.h and align.v). This can be set for each component individually, or, in case it is the same for all components inside tm_components().

tm_shape(NLD_muni) +
  tm_polygons("grey95", col = "grey90") +
tm_inset(g("A1"), group_id = "A") +
tm_inset(g("A2"), group_id = "A", width = 3, height = 3) +
tm_inset(g("A3"), group_id = "A") +
tm_layout(asp = 1) +
tm_components("A", position = tm_pos_in("left", "top", align.h = "right"), frame_combine = FALSE, equalize = FALSE, offset = 0, stack_margin = 0) 

Frame and background color and alpha

The frame and background belong to the group, not to the individual components: they are drawn around the whole group (or around each component in case frame_combine = FALSE). They can be specified via tm_components(), but also via the arguments of the component functions themselves (e.g. tm_legend(frame = FALSE)), which are passed on to the group. Note that this means that the frame and background are not part of the component itself; e.g. the frame around a chart cannot be removed via ggplot2 theme settings, but only via these arguments.

tm_shape(NLD_muni) +
  tm_polygons("grey75", col = "grey90") +
tm_inset(g("A1"), group_id = "A") +
tm_inset(g("A2"), group_id = "A", width = 3, height = 3) +
tm_inset(g("A3"), group_id = "A") +
tm_layout(asp = 1) +
tm_components("A", position = tm_pos_in("left", "bottom"), frame = TRUE, bg.color = "purple", bg.alpha = 0.5)