使用 ggplot 输出创建一个没有投影失真的栅格

问题描述

我想用 ggplot2 输出创建一个栅格 (myGeotif),但输出是投影栅格的失真,尽管范围 (lat_long <- ggplot_build(gg)$layout$panel_params[[1]][c("x_range","y_range")] ) 看起来不错:>

enter image description here

我的代码是:

#Packages
library(rgdal)
library(raster)
library(tiff)
library(ggplot2)
library(sf)

# Get data set - x any are the points
all.stands.predict<-read.csv("https://raw.githubusercontent.com/Leprechault/trash/main/prediction__bug_2021-03-18.csv")
all.stands.predict<-all.stands.predict[all.stands.predict[,3]=="VILA PALMA",] # Area selection

#Create a map
(sites <- st_as_sf(all.stands.predict,coords = c("x","y"),crs = 4326,agr = "constant"))
gg <- ggplot() +
  geom_sf(data=sites,color="grey") +
  coord_sf() +
  theme_bw() +
  theme(    
        axis.ticks=element_blank(),axis.text.x=element_blank(),axis.text.y=element_blank(),axis.title.x=element_blank(),axis.title.y=element_blank(),panel.grid.major = element_blank(),panel.grid.minor = element_blank(),panel.background = element_blank(),plot.margin = unit(c(0,0),"null"),legend.position = 'none'
       ) +
       labs(x=NULL,y=NULL)

# Save the plot
ggsave(plot=gg,"gg.tiff",device = "tiff",dpi = 300)

# Create a StackedRaster object from the saved plot
stackedRaster <- raster::stack("gg.tiff")

# Get the GeoSpatial Components
lat_long <- ggplot_build(gg)$layout$panel_params[[1]][c("x_range","y_range")] 

# Supply GeoSpatial  data to the StackedRaster 
extent(stackedRaster) <- c(lat_long$x_range,lat_long$y_range)
projection(stackedRaster) <- CRS("+init=epsg:4326")

# Create the GeoTiff
writeraster(stackedRaster,"myGeo.tif",options="PHOTOMETRIC=RGB",datatype="INT1U",overwrite=TRUE)

#GeoTIFF representation:
ggr_geotiff <- raster::stack("myGeo.tif")
image(ggr_geotiff,main="")
points(all.stands.predict$x,all.stands.predict$y,pch=16,cex=0.5,col="grey")
#

请问有什么建议可以解决吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)