【发布时间】:2020-02-28 18:47:26
【问题描述】:
我正在尝试在 r 中导入光栅 TIFF 并使用 ggplot2 绘制它。但是当我导入文件时,X 和 Y 值会以某种方式丢失。我检查了投影系统,它显示的 WGS 是正确的。程度也是正确的。我在 ArcMap 中再次查看了图像,我可以看到那里的投影(意味着它仍然存在)。
> extent(file.name)
class : Extent
xmin : 85.94994
xmax : 87.45825
ymin : 22.31023
ymax : 23.48127
> crs(file.name)
CRS arguments:
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
尽管没有 X 和 Y 值,但我首先尝试使用 nrow 和 ncol 作为 x 和 y 分别绘制它(这对我来说没有意义)。然后我尝试创建一个网格,但它引发了一个错误。无论如何,它显示了一张灰色的地图,里面什么都没有。传说是正确的。
创建网格的代码。
start_lat <- 21
start_lng <- 84
griddf <- expand.grid(latcoords = seq(from = start_lat, by = -0.01, l = ncell(file.name)), +
lngcoords = seq(from = start_lng, by = -0.01, l = ncell(file.name)))
Error: cannot allocate vector of size 24.1 Gb
这是我用于绘图的代码。
library(raster)
library(rgdal)
library(ggplot2)
file.name <- raster("path and name of the file.tif")
file_df <- as.data.frame(file.name)
ggplot() +
geom_raster(data = file_df , aes(x = nrow(file.name), y = nrow(file.name), fill = spei_03)) + #spei_03 is the name of the column whose values I want to plot.
scale_fill_viridis_c() +
coord_quickmap()
我是 R 的新手,似乎无法理解它。我在网上搜索了答案,但没有找到答案。请帮助我 1)如何将 X 和 Y 数据带入 R,以及 2)如何创建一个有效的 ggplot。
【问题讨论】:
-
您应该检查
rasterVis或tmap包以绘制栅格。 -
哇!那行得通。我刚刚使用了 plot(file.tif) 并且它起作用了。