# Plotting only raster cells that fall within boundaries of another raster in R

I've got two rasters, `soil` and `slope`. I want to plot the `slope` raster but only in areas where there is `soil` (i.e. soil is > 0). How can I do this?

This is the `soil` raster:

This is the `slope` raster:

`soil`

`````` > extent(soil)
class      : Extent
xmin       : -180
xmax       : 179.9856
ymin       : -60
ymax       : 89.994
> res(soil)
[1] 0.008333 0.008333
> origin(soil)
[1]  0.001133 -0.002400
``````

`slope`

`````` > extent(slope)
class      : Extent
xmin       : -180
xmax       : 180.0023
ymin       : -56.00016
ymax       : 84.00257
> res(slope)
[1] 0.008333386 0.008333496
> origin(slope)
[1] 0.0011330 0.0009332
``````

`````` > dim(soil)
[1] 18000 43200     1

> dim(slope)
[1] 16800 43200     1
``````
• The two rasters have the same extent. Do you mean you want the slope where the soil raster is not zero? Does zero define your idea of where "soil" isnt? Commented Mar 26, 2020 at 15:46
• Hi! Yes, exactly. I need to create a graph showing slope only in areas where soil is greater than 0. Commented Mar 26, 2020 at 15:55
• a graph or a map? Commented Mar 26, 2020 at 16:01
• A map! Sorry :) Commented Mar 26, 2020 at 16:01
• do the rasters have the same resolution, extent, and origin? Show the summary of each raster if not. Commented Mar 26, 2020 at 16:01

``````require(raster)

# make 2 dummy rasters, different extents but same origin and resolution. Values 1:6
slope <- raster(xmn = 0, xmx = 5, ymn = 0, ymx = 5, res = 1)
slope[] <- sample(1:6, ncell(slope), replace = T)

soil <- raster(xmn = 0, xmx = 7, ymn = -1, ymx = 4, res = 1)
soil[] <- sample(1:6, ncell(soil), replace = T)

# create your blank raster to populate. This is only the area both inputs occupy.
r_intersec <- raster(raster::intersect(slope,soil))

# The other 2 rasters should be cropped to the extent of your target raster, to avoid extent errors as mentioned in the comments
croped_slope <- crop(slope,extent(r_intersec))
croped_soil <- crop(soil,extent(r_intersec))

tmp <- croped_slope
tmp[which(croped_soil[] <= 0)] <- NA

par(mfrow=c(1,3))
plot(slope)
plot(soil)
plot(tmp)
``````

*raster sample and crop idea from https://gis.stackexchange.com/a/328883/77836

• This will only work if the rasters overlap perfectly, which they don't when you look at the description in the question. Commented Mar 26, 2020 at 19:46
• @Spacedman Yes, sorry. I believe I fixed it. Commented Mar 26, 2020 at 20:52
• Hi @MarujoRe thanks for your help! When I run your code on my R it throws me an error at the step to create blank raster to populate: `unable to find an inherited method for function ‘raster’ for signature ‘"integer"’`. Not sure why? Commented Mar 27, 2020 at 13:32
• Does running `raster::intersect(slope,soil))` solve the issue? This specifies that `intersect` from the raster package is to be used, in case other packages with functions of the same name is masking the one you want to use. Commented Mar 27, 2020 at 14:15
• Yep, works now! Thank you @humperderp Commented Mar 27, 2020 at 16:54