0

I'd like to create a good representation of my ggplot in geoTIFF, but the results is not so good. I try to:

#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, "in"),
                         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

I need a good resolution geoTIFF similar to my plot(gg) with the margins and all the elements present in my ggobject. Please, any ideas?

4
  • Why do you need to include the plot elements (e.g., title, axes, axis labels) in the GeoTIFF? I've never seen that done before. Commented Mar 21, 2021 at 4:24
  • 1
    @Stewart, because my final goal is the convertion of my geotiff to geoPDF and plot elements help in field location using Avenza app for example. Commented Mar 21, 2021 at 15:01
  • Did you ever manage to solve this? I'm having the same problem Commented Apr 27, 2022 at 19:44
  • Hi @SollanoRabeloBraga I'm not solving this issue. Unfortunately, this question is open yet. Commented Apr 30, 2022 at 14:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.