4

I'm trying to save a plot using 3 maps made by the tmap package, with the larger one at the top, and the other 2 at the bottom like the example above: enter image description here

But using tmap_arrange() provided by the package for this kind of procedure, it gives me the followig:

data(World)
p1 <- tm_shape(World)+tm_polygons()
p2 <- tm_shape(World[World$continent=='South America',])+tm_polygons()
p3 <- tm_shape(World[World$name=='Brazil',])+tm_polygons()

tmap_arrange(p1,p2,p3,nrow=2)

enter image description here

I've tried to use many options, like export the maps as images and then import again to R to compose a full image using par() and/or split.screen(), but also doesn't work properly. There is any way to work around this and get the wanted result?

Thanks in advance!

Luis Antolin
  • 144
  • 1
  • 7
  • I had a similar issue and played around a bit. No obvious base R solution like `par()` or `layout()` works. Also `multiplot` fails. A nested approach doesn't work either because `tmap_arrange` wants `tmap` objects. – Fitzroy Hogsflesh Oct 26 '18 at 09:14

1 Answers1

1

One hackish way would be to use the grid package functionality. Grab the output of each plot/map and store it as a gTree object and then try to arrange the new objects in a grid.

library(tmap)
library(cowplot) # for plot_grid() function - good to arrange multiple plots into a grid
library(grid)
library(gridGraphics)

data(World)

tm_shape(World) + tm_polygons()
g1 <- grid.grab()

tm_shape(World[World$continent == 'South America', ]) + tm_polygons()
g2 <- grid.grab()

tm_shape(World[World$name == 'Brazil', ]) + tm_polygons()
g3 <- grid.grab()

# Try to arrange the plots into a grid using cowplot::plot_grid().
# First bind the p2 and p3 as one plot; 
# adjust distance between them by forcing a NULL plot in between.
p23 <- plot_grid(g2, NULL, g3, rel_widths = c(1, -0.7, 1), nrow = 1)
plot_grid(g1, p23, nrow = 2, scale = c(0.8, 1))

I could not figure it out how to make it respond to the align argument though :/ But maybe this puts you in some exploring direction or others can edit/improve this answer.

# Save the plot
ggsave(filename = "tmap-arrange-grid-1.png", 
       width = 10, height = 6, units = "cm", dpi = 150)

enter image description here

Note that, initially I thought that I could explore with adding a NULL object to tmap_arrange like tmap_arrange(p1, NULL, p2, p3, nrow = 2), but unfortunately, it does not accept it.


Another approach, inspired from this related question could be something along these lines:

library(grid)

grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow = 2, ncol = 2)))
print(p1, vp = viewport(layout.pos.row = 1, layout.pos.col = 1:2))
print(p2, vp = viewport(layout.pos.row = 2, layout.pos.col = 1))
print(p3, vp = viewport(layout.pos.row = 2, layout.pos.col = 2))

enter image description here

Again, here, I didn't have the time to explore with aligning the plots perfectly, but others might improve this answer.

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68