I am trying to export a number of plots using R's tmap
package. I am quite new to using tmap
.
I have set up two maps that should cover a similar extent and I am using the bbox
argument of tm_shape
with the intent of forcing exactly the same extents. I need the extent to be the same as they will be going into a report one page after another so people can flick between them and compare without things shifting.
The problem occurs which one map is made with a shape (I've tried sp and sf shapes and polygon and points, all give the same) and the other with a raster.
I have made a reprex using the default data from tmap. In my real data the raster and shape data have different sources but they are all in the same CRS.
You can see in the two maps below that the raster one is closer to the edge especially obvious on the left and bottom. This even happens if I zoom the bbox extent out so neither is anywhere near the edge.
Anyone know why this is different or how to fix it?
library(sp)
library(raster)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(stars)
#> Loading required package: abind
library(tmap)
data(NLD_muni)
NLD_extent <- raster::extent(NLD_muni)
NLD_rast_ <- st_rasterize(NLD_muni)
st_crs(NLD_rast_) == st_crs(NLD_muni)
#> [1] TRUE
# projection(NLD_muni)
tm_shape(NLD_muni, bbox = NLD_extent,projection = st_crs(NLD_muni)) +
tm_polygons("population")
tm_shape(NLD_rast_, bbox = NLD_extent,projection = st_crs(NLD_muni)) +
tm_raster()
Created on 2020-05-08 by the reprex package (v0.3.0)