R crop raster using polygon keeping cells along the border

Viewed 1718

I'm trying to crop this raster over Italy but the output seems to miss some of the cells along the border. See areas highlighted in red in the picture below: enter image description here

How can I keep all the cells that cross the borders?

Below is my script:

library(raster)

# Load data
x <- raster("x.nc")
IT <- getData(name = "GADM", country = "Italy", level = 0)

# Mask and crop
x_masked <- mask(x, IT)
x_masked_cropped <- crop(x_masked, IT)

# Plot
plot(x_masked_cropped)
plot(IT, add = T)
2 Answers
Related