[英]How to add box layer using levelplot in R?
我想在情節中添加一些特殊范圍的框。
例
gh <- raster()
gh[] <- 1:ncell(gh)
SP <- spsample(Spatial(bbox=bbox(gh)), 10, type="random")
然后畫他們
levelplot(gh, col.regions = rev(terrain.colors(255)), cuts=254, margin=FALSE) +
layer(sp.points(SP, col = "red"))
這將繪制一個帶有多個十字的地圖,但是我需要繪制一個具有一定范圍的框:
extent(gh) = extent(c(xmn=-180,xmx=180,ymn=-90,ymx=90))
e6 <- extent( 2 , 8 , 45 , 51 )
我想在情節上加上e6
並將數字2
放在方框內。
將Extent
對象轉換為SpatialPolygons
,並使用以下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')
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.