如何解决使用 ggplot 输出创建一个没有投影失真的栅格
我想用 ggplot2 输出创建一个栅格 (myGeotif
),但输出是投影栅格的失真,尽管范围 (lat_long <- ggplot_build(gg)$layout$panel_params[[1]][c("x_range","y_range")]
) 看起来不错:>
我的代码是:
#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 举报,一经查实,本站将立刻删除。