I am having trouble. I am unable to identify the issue when plotting a SpatialPixelDataframe
and a SpatialPolygonDataframe
with the same CRS in tmaps.
The spatialpixels
object can be found here saved as RDS, and the polygons shapefile here, zipped.
Here is my attempt with base functions:
library(sf)
library(sp)
ireland <- st_read("Counties.shp")
sp_pred <- readRDS("sppred_range100_sd2.RDS")
#transform polygons into the pixels CRS
ireland_proj <- st_transform(ireland, sp_pred@proj4string)
#turn into sp object
ireland_sp <- as_Spatial(ireland_proj)
#plot with base functions
plot(sp_pred['mean'])
plot(ireland_sp, add = T)
Here is my attempt with tmap
library(tmap)
tm_shape(sp_pred) +
tm_raster("mean", palette = terrain.colors(10)) +
tm_shape(ireland_sp) +
tm_borders("black", lwd = .5) +
tm_legend(show = FALSE)
This is so simple and I can't see where I might have gone wrong, but also I can't see how it can be an error in how tmap
works!