4

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:

enter image description here

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)

enter image description here

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!

chrki
  • 6,143
  • 6
  • 35
  • 55
jkeirstead
  • 2,881
  • 3
  • 23
  • 26

1 Answers1

3

I should have guessed.... The problem was with different projections on the base map and the new layer. The new layer used a Traverse Mercator projection which would cause the memory problem on its own, if I'd remember to include the same coord_map projection as on the base layer.

You can re-create the problem by downloading these shapefiles (I used UKBORDERS, but you can also get them from CDU, which use a different projection yet again) and then doing something like:

ggplot(new_data, aes(x=long, y=lat, group=group)) + geom_polygon() +
         coord_map(proj="azequalarea")

To fix it, load the original shapefile into QGis, select Settings > Project Properties... and select the WGS 84 projection, apply and save. The new shapefile works just fine and gives me this lovely result:

enter image description here

jkeirstead
  • 2,881
  • 3
  • 23
  • 26