So, would the reference system have to be specified exactly so that this minimal difference between the raster's extent and cut doesn't occur?
And the other question, I have this example code on a plot with the Terra package of any raster to which I add the extent:
`library(terra)
# Example raster
r <- rast(system.file("ex/elev.tif", package="terra"))
# Original extent
ext_original <- ext(r)
# Expanded extent (larger on all sides)
ext_expanded <- ext(
xmin(ext_original) - 1,
xmax(ext_original) + 1,
ymin(ext_original) - 1,
ymax(ext_original) + 1
)
# Expanded raster (with NA in the new areas)
r_expanded <- extend(r, ext_expanded)
# Plot 1: original raster
plot(r, main = "Original raster with normal extent")
# Plot 2: expanded raster (larger, with NA on the edges)
plot(r_expanded, main = "Raster with expanded extent")
`
This is the raster plot without the background map.
Now to this map I want to add the "background map" from the previous code of the maptiles library but keeping the same format of the raster map with the extension, but it happens that it does not respect the format of the terra plot itself.
`library(terra)
library(maptiles)
# Example raster: global elevation
r <- rast(system.file("ex/elev.tif", package="terra"))
# Original extent and expanded extent
ext_original <- ext(r)
ext_expanded <- ext(
xmin(ext_original) - 1,
xmax(ext_original) + 1,
ymin(ext_original) - 1,
ymax(ext_original) + 1
)
# Download OSM tile with the expanded extent
osm_rast <- get_tiles(ext_expanded, provider = "OpenStreetMap", crop = TRUE, zoom = 8)
# Plot base map
plot(osm_rast, axes=FALSE, legend=FALSE)
# Plot raster on top
plot(r, add=TRUE)
` and this is the map with the "openstreetmap style background map" My question is how can I make the OpenStreetMap style map be added to the background without changing the format of the map plot without a background with the Terra library?, that is, the same map without a background with the extension, but with the background, I don't know if I'm making myself clear.Because as you can see the difference is huge and as you can see in image 2 the latitude limits seem to be "not respected"