3

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)

Sarah
  • 3,022
  • 1
  • 19
  • 40
  • Did you ever get this to work correctly? – user2711915 Apr 04 '23 at 14:59
  • 1
    I don't think so, I'm not 100% sure which project I was using this for but based on best guess on the timing of this question I had a quick look at my scripts and I made my maps using `ggplot2` and `ggspatial::layer_spatial`. If I was doing it again now I would probably use `sf::geom_sf` and either `stars` or just `ggplot2::geom_raster` I think – Sarah Apr 06 '23 at 01:22
  • Thanks. The best I could get this to do using bbox, disabling all options in the tm_layout and setting the exact resolution of the output was to get it to have an extra 10px of internal space in the x direction, but at least there was no extra space in the y direction. I'll give up and use another package. – user2711915 Apr 06 '23 at 10:07

0 Answers0