Search code examples
rplotrastershapefile

Plotting a shapefile on a raster layer in R


I want to plot a raster layer with points from a shapefile on top. I have checked previous answers on this, but i still have a problem. I can plot the point shapefile and the raster layer separately without problem, but not together.

As far as I can see they should be in same projection and location.

require(maptools) 

myproj <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
shape <- readShapeSpatial("directory/mypoints.shp", proj4string = CRS(myproj))

plot(r <- raster(listVI[200]))
plot(shape)

Solution

  • I found the answer, I will put it here for others who may encounter same problem.

    The solution is simply: (as long as raster and shapefile is in same CRS)

    plot(r)
    
    plot(shape, add = TRUE)