R用ggplot2耗尽内存绘制数据帧
R runs out of memory plotting data frame with ggplot2
我在 Fedora 31 上 运行 R,在一台配备 8Gb RAM 的 Dell XPS 笔记本电脑上。我正在尝试绘制 this GeoTIFF using ggplot2, so that I can overlay other data using code I've already written with ggplot2. I've been roughly following this lesson 以在 R 中处理栅格数据。将 TIFF 转换为 RasterLayer 到数据框后,R 程序在使用 ggplot2 加载数据框时失败,仅输出 "Killed"并退出。
下面是产生此错误的最小代码示例:
library(tidyverse)
library(raster)
library(rgdal)
afg_pop <- raster("afg_ppp_2020.tif")
pop_df <- as.data.frame(afg_pop, xy = TRUE)
ggplot() +
# This is the line that results with the error: "Killed"
geom_raster(data = pop_df , aes(x = x, y = y, fill = afg_ppp_2020))
运行 dmesg
显示 R 运行 内存不足:
[20563.603882] Out of memory: Killed process 42316 (R) total-vm:11845908kB, anon-rss:6878420kB, file-rss:4kB, shmem-rss:0kB, UID:1000 pgtables:19984kB oom_score_adj:0
我很难相信即使有这么大的数据文件 R 也 运行 超出了处理它所需的内存。为什么 R 需要这么多内存来执行此任务,更重要的是我可以使用什么其他方法来绘制此数据,最好使用 ggplot2?
我是 R 的新手,所以如果我忽略了一些明显的地方,请原谅我。如有任何帮助,我们将不胜感激!
我不能说 ggplot
的内存要求,但数据的空间分辨率非常高(~ 90m)。要求 ggplot 绘制 10955(行)* 17267(列)= 189,159,985 像素是没有意义的,因为您将无法看到它们(除非,也许您正在打印广告牌)。因此,一个简单的解决方法是采用常规样本,或汇总
f <- "ftp://ftp.worldpop.org.uk/GIS/Population/Global_2000_2020/2020/AFG/afg_ppp_2020.tif"
if (!file.exists(basename(f))) download.file(f, basename(f), mode="wb")
library(raster)
afg_pop <- raster("afg_ppp_2020.tif")
pop_df <- data.frame(sampleRegular(afg_pop, 10000, xy=TRUE))
library(ggplot2)
ggplot() + geom_raster(data = pop_df , aes(x = x, y = y, fill = afg_ppp_2020))
一个更好的选择,需要更长的时间
afg_pop2 <- aggregate(afg_pop, 10) # this takes some time
pop_df2 <- as.data.frame(afg_pop2, xy=TRUE)
ggplot() + geom_raster(data = pop_df2 , aes(x = x, y = y, fill = afg_ppp_2020))
地图不是很好;其他 R 包中有更好的选项来制作地图。
我在 Fedora 31 上 运行 R,在一台配备 8Gb RAM 的 Dell XPS 笔记本电脑上。我正在尝试绘制 this GeoTIFF using ggplot2, so that I can overlay other data using code I've already written with ggplot2. I've been roughly following this lesson 以在 R 中处理栅格数据。将 TIFF 转换为 RasterLayer 到数据框后,R 程序在使用 ggplot2 加载数据框时失败,仅输出 "Killed"并退出。
下面是产生此错误的最小代码示例:
library(tidyverse)
library(raster)
library(rgdal)
afg_pop <- raster("afg_ppp_2020.tif")
pop_df <- as.data.frame(afg_pop, xy = TRUE)
ggplot() +
# This is the line that results with the error: "Killed"
geom_raster(data = pop_df , aes(x = x, y = y, fill = afg_ppp_2020))
运行 dmesg
显示 R 运行 内存不足:
[20563.603882] Out of memory: Killed process 42316 (R) total-vm:11845908kB, anon-rss:6878420kB, file-rss:4kB, shmem-rss:0kB, UID:1000 pgtables:19984kB oom_score_adj:0
我很难相信即使有这么大的数据文件 R 也 运行 超出了处理它所需的内存。为什么 R 需要这么多内存来执行此任务,更重要的是我可以使用什么其他方法来绘制此数据,最好使用 ggplot2?
我是 R 的新手,所以如果我忽略了一些明显的地方,请原谅我。如有任何帮助,我们将不胜感激!
我不能说 ggplot
的内存要求,但数据的空间分辨率非常高(~ 90m)。要求 ggplot 绘制 10955(行)* 17267(列)= 189,159,985 像素是没有意义的,因为您将无法看到它们(除非,也许您正在打印广告牌)。因此,一个简单的解决方法是采用常规样本,或汇总
f <- "ftp://ftp.worldpop.org.uk/GIS/Population/Global_2000_2020/2020/AFG/afg_ppp_2020.tif"
if (!file.exists(basename(f))) download.file(f, basename(f), mode="wb")
library(raster)
afg_pop <- raster("afg_ppp_2020.tif")
pop_df <- data.frame(sampleRegular(afg_pop, 10000, xy=TRUE))
library(ggplot2)
ggplot() + geom_raster(data = pop_df , aes(x = x, y = y, fill = afg_ppp_2020))
一个更好的选择,需要更长的时间
afg_pop2 <- aggregate(afg_pop, 10) # this takes some time
pop_df2 <- as.data.frame(afg_pop2, xy=TRUE)
ggplot() + geom_raster(data = pop_df2 , aes(x = x, y = y, fill = afg_ppp_2020))
地图不是很好;其他 R 包中有更好的选项来制作地图。