保存 ggplot 地图在保存到 geoTIFF 后会产生不好的结果

问题描述

我想在 geoTIFF 中创建我的 ggplot 的良好表示,但结果不太好。我尝试:

#Packages
library(rgdal)
library(raster)
library(tiff)
library(ggplot2)
library(ggspatial)
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="red") +
  annotation_north_arrow(location = "bl",which_north = "true",pad_x = unit(0.3,"in"),pad_y = unit(0.5,style = north_arrow_fancy_orienteering) + #Add a north arrow
  annotation_scale(location = "bl",width_hint = 0.55) + #Add a scale bar
  xlab("Latitude") + ylab("Longitude") +
  coord_sf() +
  theme_bw() 


# I inspected the map created
plot(gg)

map1

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

# Create a StackedRaster object from the saved plot
stackedRaster <- 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("+proj=longlat +datum=wgs84")

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

#GeoTIFF representation:
ggr_geotiff <- stack("myGeoTiffgg.tif")
image(ggr_geotiff)

map2

我需要一个分辨率良好的 geoTIFF,类似于我的 plot(gg),带有边距和我的 gg 对象中存在的所有元素。拜托,有什么想法吗?

解决方法

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

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

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