2

I have two rasters with different extents that overlap partly:

ext1 <- extent(99500,700500,249500,600500)
ext2 <- extent(1356,643990,269903,690810)

#create rasters
rr1 <- raster(matrix(runif(1000000,0,100), 1000, 1000))
rr2 <- raster(matrix(runif(1000000,0,100), 1000, 1000))

extent(rr1) <- ext1
extent(rr2) <- ext2

How can I plot both rasters in one plot?

I've tried

ext3 <- extent(min(ext1[1],ext2[1]),max(ext1[2],ext2[2]),
               min(ext1[3],ext2[3]),max(ext1[4],ext2[4]))
plot(rr1, ext = ext3)

But the only the extent of rr1 is used.

1

Merge the extents (which saves all that min/max stuff!) and plot the extent, then add the rasters:

em = merge(extent(rr1),extent(rr2))
plot(em, type="n")
plot(rr1,add=TRUE, legend=FALSE)
plot(rr2, add=TRUE, legend=FALSE)

Use type="n" so a box isn't drawn round the extent. Use legend=FALSE or you get two overlapping legends.

From experiments it seems to me that plot(r, ext=e) only has an effect if e is within r, in other words for focusing on a part of a raster, not zooming out. The help sort of implies this:

 ext: An extent object to zoom in a region (see also ‘zoom’ and
      ‘crop(x, drawExtent())’

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.