Example:
I want to plot two tmap plots side by side, which are generated by this code.
library(tmap)
library(gridExtra)
data(World)
I was wondering if in the meantime it has become possible to plot the 5 maps in the following example side by side without switching to tm_facets(ncol=5)
.
data(NLD_prov, NLD_muni)
tmap_mode("plot")
tm_shape(NLD_muni) +
tm_fill(c("pop_0_14", "pop_15_24", "pop_25_44", "pop_45_64", "pop_65plus"),
style="kmeans",
palette=list("Oranges", "Greens", "Blues", "Purples", "Greys"),
title=c("Population 0 to 14", "Population 15 to 24", "Population 25 to 44",
"Population 45 to 64", "Population 65 and older")) +
tm_shape(NLD_prov) +
tm_borders() +
tm_format_NLD(frame = TRUE, asp=0)
Nice question!
grid.arrange
doesn't support tmap plots (yet?) in the same way it supports ggplot2
plots.
There are two options:
1) Use small multiples by assigning two values to an aesthetic (see examples from tm_facets
). Your plots don't use aesthetics, but you can trick this as follows:
tm_shape(World, projection = "merc") +
tm_fill(col=c("white", "white")) +
tm_layout("", inner.margins=c(-1.72, -2.05, -0.75, -1.56)) +
tm_borders(alpha = 0.3, lwd=2)
2) Use the grid
package to define viewports:
library(grid)
grid.newpage()
pushViewport(viewport(layout=grid.layout(1,2)))
print(plot1, vp=viewport(layout.pos.col = 1))
print(plot2, vp=viewport(layout.pos.col = 2))
Another thing, rather than clipping the shape with negative inner margins, you could also use the bounding box arguments inside tm_shape
:
tm_shape(World, projection = "merc", xlim=c(-2e6, 6.5e6), ylim=c(-4e6, 8.5e6)) +
tm_borders(alpha = 0.3, lwd=2)
It produces the same map, but it the code a little cleaner.
tmap_arrange()
https://cran.r-project.org/web/packages/tmap/tmap.pdf
data(World)
w1 <- qtm(World, projection = "eck4", title="Eckert IV")
w2 <- qtm(World, projection = "merc", title="Mercator")
w3 <- qtm(World, projection = "wintri", title="Winkel-Tripel")
w4 <- qtm(World, projection = "robin", title="Robinsin")
current.mode <- tmap_mode("plot")
tmap_arrange(w1, w2, w3, w4)
tmap_mode(current.mode)