How to rasterize SpatialPolygons in R?

Your line ras_sub0<-rasterize(subarea0,raster_bath) is just taking the index number of the polygons and assigning that to the values of the raster.

If you want just the intersection of your polygon and the raster:

subarea0_bathy <- intersect(raster_bath, subarea0)

Update: As @GodinA notes, looks like intersect() sometimes doesn't return a raster that has the complete extent of the polygon! To get around this, you can intersect with a slightly larger raster than your original:

r2 <- raster() # create a generic raster
extent(r2) <- extent(subarea0) + 1 # add 1 degree of extent (0.5 each side)
r3 <- intersect(raster_bath, r2) 

Tags:

Overlay

R

Raster