Saving a tmap with a basemap as an image?
Asked Answered
S

2

9

I have created a map in R using tmap, with a basemap, and want to save it as an image.

Whenever I use tmap_save to save it as a .png, I lose the basemap.

Presumably, this is because the basemap is not visible in plot mode and only in view mode.

Is there a good way of saving the map, with the basemap, as an image?

I have a workaround that involves saving it as HTML first, but it's not a great solution.

Speech answered 6/9, 2018 at 16:23 Comment(1)
It will be easier for us to help if you provide an example with data that we can access. I have had a similar issue but I ended up using ggplot for the plotting and ggmap to get a basemap. But I would be interested if it is doable with in tmapGunzburg
M
9

I ran into a similar issue trying to create a static map. I've found the tmaptools::read_osm function seems to solve it for me. Here's a reproducible example of three maps:

  • map_a looks good, until you save it
  • map_b doesn't look good at all
  • map_c looks good, even when saved

Create an example map in interactive view. But in the saved file, the basemap doesn't appear.

library(tmaptools) 
library(tmap)
data(metro)

tmap_mode("view")
map_a <- tm_basemap("OpenStreetMap.Mapnik") +
  tm_shape(metro) +
  tm_bubbles(size = "pop2020", col = "red") 
map_a
tmap_save(map_a, filename="map_a.png")

Try to create a similar map as a plot, but basemap doesn't appear at all, and the red dots float without a map.

tmap_mode("plot")
map_b <- tm_basemap("OpenStreetMap.Mapnik") +
  tm_shape(metro) +
  tm_bubbles(size = "pop2020", col = "red") 
map_b
tmap_save(map_b, filename="map_b.png")

Here is a solution: use read_osm. The basemap appears and can be saved.

c_osm <- tmaptools::read_osm(bb(metro), ext = 1.05)
map_c <- tm_shape(c_osm) +
  tm_rgb() +
  tm_shape(metro) + 
  tm_bubbles(size = "pop2020", col = "red") 
map_c
tmap_save(map_c, filename="map_c.png")
Metal answered 22/10, 2018 at 20:24 Comment(2)
I ran the code just now. I ran into an error on the third option, but resolved it by adding a call to "bb" before processing the read_osm. Other than that, the bubbles seemed to render. I also tried the mapshot() option offered in the solution posted by @rm1104, and it worked for me.Metal
's last solution is great!Hohenstaufen
F
7

Another option is to save the tmap object as a leaflet object and then use the mapshot() function in mapview package to save it as an image. Below is an example.

library(mapview)
#May need this for mapshot to work
#webshot::install_phantomjs()

data("World")

map <- tm_basemap(leaflet::providers$Esri.WorldTopoMap) + 
tm_shape(World) + tm_polygons(alpha = 0,border.col = "black", lwd = 2)

lf <- tmap_leaflet(map)
mapshot(lf, file = "world_map.png")
Felony answered 1/4, 2019 at 19:59 Comment(2)
mapview::mapshot(lf, file = "world_map.png") Could not load c:%5CUsers%5CKSER~1%5CAppData%5CLocal%5CTemp%5C4%5CRtmpYFNniB%5Cfile5fc82aa164bf.html Error in (function (url = NULL, file = "webshot.png", vwidth = 992, vheight = 744, : webshot.js returned failure value: 1Newmann
I am getting the above error while doing thisNewmann

© 2022 - 2024 — McMap. All rights reserved.