Question

What is the best way to make a map of large extent and high resolution, preferably in ggmap, without running out of RAM?

Background: I'm trying to make a large map of my city with ggmap. I say large because I want the map to be detailed enough that the saved pdf of it can be zoomed way in and street names can still be read. I think a zoom level of about 15 will accomplish that.

The problem is that I keep crashing my computer by running out of memory. Are there any secrets to getting something like this to work? The map I want to download has ~1100 tiles. I can get it downloaded and even plotted in R, but trying to save it crashes R. I could see doing it in pieces and then stitching it in something like photoshop, but this seems silly. It seems to me like ggmap (and ggplot2) are doing a lot of stuff in memory, and I'm wondering whether there is a way to write these files straight to disk.

Thanks for any help you can offer!

Was it helpful?

Solution

I think you have two issues here:

  1. You're plotting a raster image with a low pixel density (so zooming in just gives you poor quality images).

One solution is to use the ggsave() function in ggplot2 to output to scalar vector graphics (.svg).

For example:

library(ggplot2)
# Create dummy data
y=data.frame(matrix(rnorm(10000,5),ncol=2))
paste(object.size(y)/1e6,"mB")

# Save to image file
image=ggplot(data=x,aes(x=X1,y=X2))+geom_point()

# Write image to file
ggsave(filename="test.svg",plot=image,width=10,height=8,units="cm")

If you don't want an .svg file, I'd stick to a .png file. You can look at the res attribute in png() here

2. You're plotting something very large all at once.

This is where things get interesting. You could look at creating an image using a layered approach. That is: plot the polygons/lines/points from the large data set by their id/groupings (I'm not sure exactly how your data looks - hopefully this helps). I've given a demo for some inspiration:

# Write to .svg
enter code here

# Make dummy data
test_data=data.frame(matrix(c(1,2.5,3,3,1.5,2,2,2,1,1,3,1,0,1),ncol=2))
test_data$id=c('a','a','a','a','b','b','b')

#Plot data
plot(c(1,2),c(3,3),xlim=c(0,4),ylim=c(0,4),pch=15,col='red')

#Plot layers in big dataframe
by(test_data[,c("X1","X2")],INDICES=test_data$id,polygon)

The by() function is very handy for producing multi-layer plots. It's significantly faster than a for() loop in R.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top