Search code examples
rplotsavegisggmap

Saving ggmap object to a file which can be reused?


Is it possible to save a ggmap to a local file?

Context: I need a high resolution map of a big region, which takes the stamen server quite a long time to get. I think it would be better, to get the map once, save it to a file and from then on, work on the local copy.

My code for getting the map is:

library(ggmap)
map <- get_stamenmap(bbox = c(left = 8.7, bottom = 46.8, right = 9.7, top = 47.6), zoom = 14, maptype = c("toner"))

I know I can save it as an image for example with:

png(filename=name, width=1280, height=1280)
print(map)
dev.off()

But this doesn't help, because I want to use the map later on to plot different points and polygons on it, which is only possible if the map knows the 'lat' and 'lon' of every point on the picture.

So, is there a way to save the map in it's raw data form?


Solution

  • As suggested by user3710546 save the ggmap object to a RData file with save function, and then, read it back with load. For example:

    library(ggmap)
    map <- get_map(location="California", zoom=6, maptype="terrain")
    
    save(map, file = "my_map.RData")
    load(file = "my_map.RData")