Plotting a raster behind a shapefile

后端 未结 1 1289
太阳男子
太阳男子 2021-02-05 23:32

How can I plot a \"raster\" object behind a shapefile object? Both plot fine on their own but the points don\'t plot over the raster:

require(rgdal)
require(mapt         


        
相关标签:
1条回答
  • 2021-02-05 23:52

    Overplotting raster plots with points, lines, and polygons should work just fine, as the following example shows.

    My best guess would be that the Spatial* objects you are attempting to plot on top of the raster fall outside of the region being plotted. Have you checked that both the raster and Spatial* objects are in the same CRS, and (assuming they are) that the bounding boxes overlap? (i.e. try bbox(shp) and bbox(ras), and compare the results).

    library(rgdal)
    library(raster)
    # Create a raster
    ras <- raster(ncols=36, nrows=18)
    ras[] <- runif(ncell(ras))
    # Create a SpatialPoints object
    shpPts <- spsample(Spatial(bbox=bbox(ras)), 20, type="random")
    # Create a SpatialPolygons object
    p1 <- rbind(c(-10,0), c(140,60), c(160,0), c(140,-55), c(-10,0))
    shpPolys <- SpatialPolygons( list(Polygons(list(Polygon(p1)), 1)))
    
    # Plot them, one layer after another
    plot(ras)
    plot(shpPts, pch=16, col="red", add=TRUE)
    plot(shpPolys, col="yellow", add=TRUE)
    

    enter image description here

    0 讨论(0)
提交回复
热议问题