r - ggplot map 的奇怪内存问题

标签 r memory ggplot2 gis shapefile

我正在尝试制作一些不列颠群岛的 map ,但遇到了一个非常奇怪的内存问题。我的工作流程是利用 ggplot 的图层将新细节添加到 basemap 上。

basemap 本身采用来自 GADM 的英国和爱尔兰的形状文件, 使用 thinnedSpatialPoly 简化几何图形在 MapTools 中,生成此 map :

enter image description here

然后对于后续图层,我做同样的事情:加载 SHP 文件,简化几何并将其添加到 basemap ,如下所示:

# new_data is a SpatialPolygonsDataFrame
base_map + geom(data=new_data, color="black", fill=my_fill)

enter image description here

对于大多数 map ,我都做得很好。但是,当我尝试添加一个特定层时,R 卡住并最终给我以下错误:
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)

生成此图的代码与上述完全相同。如果我自己绘制新图层,例如
ggplot(new_data, aes(x=long, y=lat, group=group)) + geom_polygon

那么就没有问题了, map 绘制得非常快。作为磁盘引用,形状文件为 769 KB,而其他层为 248 KB。

我不知道如何调试和解决这个问题。任何指针都会很棒 - 谢谢!

最佳答案

我应该猜到了.... 问题在于 basemap 和新图层上的投影不同。新图层使用了 Traverse Mercator 投影,如果我记得包含相同的 coord_map,它会自行导致内存问题。投影作为基础层。

您可以通过下载这些 shapefile(我使用 UKBORDERS ,但您也可以从 CDU 再次使用不同的投影)来重新创建问题,然后执行以下操作:

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

要修复它,请将原始 shapefile 加载到 QGis 中,选择 Settings > Project Properties...并选择 WGS 84投影,应用和保存。新的 shapefile 工作得很好,给了我这个可爱的结果:

enter image description here

关于r - ggplot map 的奇怪内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16013445/

相关文章:

r - 如何更改 ggplot geom_histogram() 中的直方图边界线厚度

c++ - 将指针插入原始内存

assembly - 为什么用MSB作为符号位?

c# - WPF 列表框项目消耗太多内存

r - 突出显示悬停时组中的所有值

r - 在包中使用 ggplot2 时,讨厌的 ggplot2 命名空间冲突

r - 使用fir {seewave}选择音频文件的频率范围

r - 在 R 中传递大型矩阵的替代方法

html - RMarkdown html _site.yml 导航栏 href 链接,在新标签页中打开,目标为 ="_blank"

r - ggplot geom_point 绘制两个分类变量并填充缺失