簡體   English   中英

如何在R中使用levelplot添加框層?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM