Convert the Extent
object into a SpatialPolygons
, and extract its centroid with coordinates
:
library("raster")
library("sp")
library("rasterVis")
gh <- raster()
gh[] <- 1:ncell(gh)
SP <- spsample(Spatial(bbox=bbox(gh)), 10, type="random")
e6 <- extent( 2, 8, 45, 51)
e6pol <- as(e6, 'SpatialPolygons')
centroid <- coordinates(e6pol)
levelplot(gh, col.regions = rev(terrain.colors(255)), cuts=254, margin=FALSE) +
layer({sp.points(SP, col = "red")
sp.polygons(e6pol)
panel.text(centroid[,1], centroid[,2], '2')
})