R 使用ggplot2绘制TIFF数据
我正在尝试在r中导入光栅TIFF,并使用ggplot2进行打印。但是当我导入文件时,不知何故X和Y值丢失了。我检查了投影系统,它显示的是正确的WGS。范围也是正确的。我再次在ArcMap中查找图像,我可以看到那里的投影(这意味着它仍然在那里) 尽管没有X和Y值,但我首先尝试使用R 使用ggplot2绘制TIFF数据,r,ggplot2,R,Ggplot2,我正在尝试在r中导入光栅TIFF,并使用ggplot2进行打印。但是当我导入文件时,不知何故X和Y值丢失了。我检查了投影系统,它显示的是正确的WGS。范围也是正确的。我再次在ArcMap中查找图像,我可以看到那里的投影(这意味着它仍然在那里) 尽管没有X和Y值,但我首先尝试使用nrow和ncol作为X和Y resp来绘制它(这对我来说没有意义)。然后我尝试创建一个网格,但它抛出了一个错误。不管怎样,它显示的是一张灰色地图,里面什么都没有。这些传说是正确的 用于创建网格的代码 start_lat
nrow
和ncol
作为X和Y resp来绘制它(这对我来说没有意义)。然后我尝试创建一个网格,但它抛出了一个错误。不管怎样,它显示的是一张灰色地图,里面什么都没有。这些传说是正确的
用于创建网格的代码
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
start\u lat除了基本R plot函数外,这里还有三个用于打印光栅的选项。在第一个和第二个选项中,您可以直接打印光栅图像,而第三个选项需要从光栅到data.frame的转换
rasterVis
package
tmap
package
ggplot2
package
最后,使用ggplot2
需要将光栅
转换为data.frame
,然后进行打印。我不建议使用此选项,因为您需要创建非常大的数据帧(并且有更简单的方法获得此图)
库(ggplot2)
temp您应该检查用于绘制光栅的rasterVis
或tmap
包。哇!成功了。我刚刚使用了plot(file.tif),它就工作了。
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()
library(rasterVis)
gplot(image) +
geom_tile(aes(fill = value))
library(tmap)
tm_shape(image)+
tm_raster()
library(ggplot2)
temp<-as.data.frame(image, xy = T)
ggplot(temp, aes(x = x, y = y, fill = image))+
geom_raster()