I'm trying to make some maps of the British Isles and have come across a very strange memory problem. My workflow is taking advantage of ggplot's layers to add new detail onto a base map.
The base map itself takes shape files for the UK and Ireland from GADM, simplifies the geometries using thinnedSpatialPoly
in MapTools, resulting in this map:
Then for subsequent layers, I do the same thing: load in the SHP file, simplify the geometry and add it to the base map, as in:
# new_data is a SpatialPolygonsDataFrame
base_map + geom(data=new_data, color="black", fill=my_fill)
For most of the maps, I'm making this works just fine. However when I try to add one particular layer, R freezes and eventually gives me the following error:
Error: cannot allocate vector of size 86.9 Mb
In addition: Warning messages:
1: In data.frame(x = x, y = y, aes_df) :
Reached total allocation of 3953Mb: see help(memory.size)
2: In data.frame(x = x, y = y, aes_df) :
Reached total allocation of 3953Mb: see help(memory.size)
3: In as.data.frame.numeric(x[[i]], optional = TRUE) :
Reached total allocation of 3953Mb: see help(memory.size)
4: In as.data.frame.numeric(x[[i]], optional = TRUE) :
Reached total allocation of 3953Mb: see help(memory.size)
5: In as.data.frame.numeric(x[[i]], optional = TRUE) :
Reached total allocation of 3953Mb: see help(memory.size)
6: In as.data.frame.numeric(x[[i]], optional = TRUE) :
Reached total allocation of 3953Mb: see help(memory.size)
The code for generating this plot is exactly the same as above. If I plot the new layer on its own, e.g.
ggplot(new_data, aes(x=long, y=lat, group=group)) + geom_polygon
then there is no problem and the map is drawn very quickly. For reference on disk, the shape file is 769 KB versus 248 KB for the other layers.
I'm at a loss here as to how to debug and fix this. Any pointers would be great - thanks!