Search code examples
rplotmappingraster

How to plot two rasters with different extensions


I am trying to plot two rasters with different extensions (from two different areas) and they are getting superimposed.

First raster TN:

class: RasterLayer

dimensions : 1785, 2363, 4217955 (nrow, ncol, ncell)

resolution : 0.11766, 0.11766 (x, y)

extent : 474953.5, 475231.5, 6539165, 6539375 (xmin, xmax, ymin, ymax)

coord. ref. : +proj=lcc +lat_1=58 +lat_2=59.33333333333335 +lat_0=57.51755393055556 +lon_0=24 +x_0=500000 +y_0=6375000 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs

data source : C:\Users\Usuario\AppData\Local\Temp\RtmpSYhw1w\raster\r_tmp_2018-11-29_153551_4484_16660.grd

names : layer

values : 1, 3 (min, max)

Second raster TS:

class : RasterLayer

dimensions : 7266, 5237, 38052042 (nrow, ncol, ncell)

resolution : 0.1141, 0.1141 (x, y)

extent : 474817, 475414.5, 6537431, 6538260 (xmin, xmax, ymin, ymax)

coord. ref. : +proj=lcc +lat_1=58 +lat_2=59.33333333333335 +lat_0=57.51755393055556 +lon_0=24 +x_0=500000 +y_0=6375000 +ellps=GRS80 +towgs84=0,0,0,-0,-0,-0,0 +units=m +no_defs

data source : C:\Users\Usuario\AppData\Local\Temp\RtmpSYhw1w\raster\r_tmp_2018-11-29_154600_4484_99649.grd

names : layer

values : 1, 2 (min, max)

library(raster)
ext1 <- extent(6600000, 7000000, 66000000, 70000000) #total area extent

extent(TN) <- ext1
extent(TS) <- ext1 #rasters with the same extent (total area)

em = merge(extent(TN),extent(TS))
plot(em, type="n")
plot(TN,add=TRUE, legend=FALSE)
plot(TS, add=TRUE, legend=FALSE)

maps superimposed


Solution

  • The plots are overlapping just because you have the same extents for each raster. Give them different extents. Here's a reproducible example:

    library(raster)
    logo <- raster(system.file("external/rlogo.grd", package="raster")) 
    logo1 <- logo
    logo2 <- logo
    

    Assign the correct extents and merge:

    ext1 <- extent(0, 45, 0, 45)
    ext2 <- extent(55, 100, 55, 100)
    extent(logo1) <- ext1
    extent(logo2) <- ext2
    em <- merge(ext1, ext2)
    

    Add the data to a plot:

    plot(em, type = "n")
    plot(logo1, add = T, legend = F)
    plot(logo2, add = T, legend = F) 
    

    non-overlapping rasters