Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R使用ggplot2打印数据帧时内存不足_R_Linux_Ggplot2_R Raster - Fatal编程技术网

R使用ggplot2打印数据帧时内存不足

R使用ggplot2打印数据帧时内存不足,r,linux,ggplot2,r-raster,R,Linux,Ggplot2,R Raster,我在Fedora 31上运行R,在一台配备8Gb RAM的戴尔XPS笔记本电脑上运行。我正在尝试使用ggplot2进行绘图,这样我就可以使用已经使用ggplot2编写的代码覆盖其他数据。我大致上一直在关注R中的光栅数据。在将TIFF转换为光栅层并转换为数据帧后,R程序在使用ggplot2加载数据帧时失败,只需输出“Killed”并退出 以下是产生此错误的最小代码示例: library(tidyverse) library(raster) library(rgdal) afg_pop <-

我在Fedora 31上运行R,在一台配备8Gb RAM的戴尔XPS笔记本电脑上运行。我正在尝试使用ggplot2进行绘图,这样我就可以使用已经使用ggplot2编写的代码覆盖其他数据。我大致上一直在关注R中的光栅数据。在将TIFF转换为光栅层并转换为数据帧后,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))
我很难相信,即使有一个数据文件,这么大的R也会耗尽处理它所需的内存。为什么R需要这么多内存来执行此任务,更重要的是,我可以使用什么其他方法来绘制此数据,最好使用ggplot2


我对R比较陌生,所以如果我忽略了一些显而易见的事情,请原谅我。任何帮助都将不胜感激

我无法说明
ggplot
的内存要求,但数据的空间分辨率非常高(~90m)。要求ggplot绘制10955(行)*17267(列)=189159985像素是没有意义的,因为您将无法看到它们(除非您正在打印广告牌)。因此,一个简单的解决方法是获取常规样本,或者进行聚合

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))

f谢谢,罗伯特,这对我很有效。我想知道还有哪些软件包更适合制作这样的地图;我的印象是,ggplot是这种分层GIS工作的良好选择。
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))