With the tmap package, thematic maps can be generated with great flexibility. The syntax for creating plots is similar to that of
ggplot2. The tmap package also contains many facility functions for reading and processing shape files (see overview). This vignette will focus on the core business of tmap, which is plotting maps. It’s also possible to plot maps interactively; see
We refer to shape objects as objects from the class
Raster, respectively from the
sp and the
raster package. The supported subclasses are:
|Without data||With data|
Obviously, shape objects with data (the right-hand side column) are recommended, since data is what we want to show.
Load shape object of Europe (contained in this package):
Shape objects in ESRI format can be read with
read_shape and written with
write_shape. Projection can be get and set with
set_projection respectively. Note: projections can also directly (and temporarily) be set in the plotting method (as argument of
tm_shape, see below).
The plotting syntax is based on that of
qtm function is
tmap’s equivalent to
qplot. The first, and only required argument is a shape object:
So, by default, the polygons (in case the shape object is a SpatialPolygonsDataFrame) are filled with light grey, and the polygon borders are drawn in dark grey.
A choropleth is created with the following code:
qtm(Europe, fill="well_being", text="iso_a3", text.size="AREA", root=5, fill.title="Well-Being Index", fill.textNA="Non-European countries", format="Europe", style="gray")
In this code,
text serve as aesthetics. Both
iso_a3 are variables of the data contained in the shape object
Europe. A color palette, in this case the qualitative palette from yellow to red, is mapped to the values of
well_being. The variable
iso_a3 contains the text labels, in this case the country codes. The arguments
root determine the fontsizes of the text labels (in this case, the fifth root of the area sizes are taken). The
fill.title argument is the title for the
fill-legend. The argument
fill.textNA is the legend text for missing values. The final two arguments,
style are predefined layout settings for this shape object (see layout).
qtm offers the same flexibility as the main plotting method (to be described next). However, for more complex plots, the main plotting method is recommended for tis readability.
The main plotting method, the equivalent to
ggplot, consists of elements that start with
tm_. The first element to start with is
tm_shape, which specifies the shape object. Next, one, or a combination of the following drawing layers should be specified:
||Fills the polygons||col|
||Draws polygon borders||col, lwd|
||Draws bubbles||size, col|
||Draws polylines||col, lwd|
||Draws a raster||col|
||Add text labels||text, size, col|
Both constant values as well as data variable names can be assigned to the aesthetic arguments (except for
tm_borders). For instance,
tm_fill(col="blue") colors all polygons blue, while
"var1" is the name of a data variable in the shape object, creates a choropleth. If a vector of constant values or variable names are provided, small multiples are created.
The following layers are map attributes:
||Add coordinate grid lines|
||Add credits text label|
||Add map compass|
||Add scale bar|
The last plot is reproduced as follows:
tm_shape(Europe) + tm_fill("well_being", textNA="Non-European countries", title="Well-Being Index") + tm_borders() + tm_text("iso_a3", size="AREA", root=5) + tm_format_Europe() + tm_style_grey()
We refer to
tm_shape and its subsequent drawing layers as a group. Multiple groups can be stacked. To illustrate this, let’s create a topographic map of Europe:
data(land, rivers, metro) tm_shape(land) + tm_raster("trees", breaks=seq(0, 100, by=20), legend.show = FALSE) + tm_shape(Europe, is.master = TRUE) + tm_borders() + tm_shape(rivers) + tm_lines(lwd="strokelwd", scale=5, legend.lwd.show = FALSE) + tm_shape(metro) + tm_bubbles("pop2010", "red", border.col = "black", border.lwd=1, size.lim = c(0, 11e6), sizes.legend = c(1e6, 2e6, 4e6, 6e6, 10e6), title.size="Metropolitan Population") + tm_text("name", size="pop2010", scale=1, root=4, size.lowerbound = .6, bg.color="white", bg.alpha = .75, auto.placement = 1, legend.size.show = FALSE) + tm_format_Europe() + tm_style_natural()
Things to learn from this code:
tm_shape, but in this case in the second
is.master=TRUE. Notice that the other shapes, i.e.
metroalso contains outside Europe: see for instance
tm_layoutcontrols all layout options such as fonts, legends, margins, and colors. The element
tm_format_Europeis a wrapper function with some other defaults that are tailored for Europe: for instance, the legend is placed top right. The element
tm_layout_naturalis another wrapper function of
tm_layoutused to specify map-independent layout layout settings, such as default colors. See also layout.
Small multiples are generated in two ways:
tm_shape(Europe) + tm_polygons(c("pop_est_dens", "gdp_cap_est"), style="kmeans", title=c("Population density", "GDP per capita")) + tm_format_Europe() + tm_style_grey()
tm_shape(Europe) + tm_polygons("well_being", title="Well-Being Index") + tm_facets("part") + tm_style_grey()
Notice that this plot has a different layout than the previous one. In the last plot, panels are used, and the legend is shown next to the maps. These options can be changed with the arguments
tm_layout. By default, the panel/external legend layout is used when the group-by variable is specified, since in that case, the multiples share a common legend.
The scales of each aesthetic argument can be set to either fixed or free, and also, the coordinate ranges of the small multiples:
tm_shape(Europe[Europe$continent=="Europe",]) + tm_fill("part", legend.show = FALSE) + tm_facets("name", free.coords=TRUE, drop.units=TRUE)
drop.units is used to drop all non-selected spatial units. If
drop.shapes=FALSE then neighboring countries are also visible.
The layout of the thematic map can be changed with
tm_layout or one of its wrapper functions. In the next example we use two of these wrapper functions, one for the overall format of world maps, and one for the legend.
data(land) data(World) pal8 <- c("#33A02C", "#B2DF8A", "#FDBF6F", "#1F78B4", "#999999", "#E31A1C", "#E6E6E6", "#A6CEE3") tm_shape(land, ylim = c(-88,88), relative=FALSE) + tm_raster("cover_cls", palette = pal8, title="Global Land Cover", legend.hist=TRUE, legend.hist.z=0) + tm_shape(World) + tm_borders() + tm_format_World(inner.margins=0) + tm_legend(text.size=1, title.size=1.2, position = c("left","bottom"), bg.color = "white", bg.alpha=.2, frame="gray50", height=.6, hist.width=.2, hist.height=.2, hist.bg.color="gray60", hist.bg.alpha=.5)