2016-08-13 24 views
2

我有一個來自RasterStack的八個地圖面板,並且希望爲該圖添加一個100km的比例尺。我已經在spplot函數中使用sp.layout在柵格頂部添加了SpatialLines對象,並假定使用比例尺(或北方箭頭)也可以實現,但我無法獲得任何工作。如何添加一個比例尺到spplot中的單個面板

我使用的代碼是:

library(sp) 
library(raster) 
library(grid) 

spplot(rasterstack, layout=c(2,4), 
     names.attr=c("a", "b", "c", "d", "e", "f", "g", "h"), 
     between=list(x=1, y=1), 
     at=seq(0,1000,by=100), 
     col.regions=colorRampPalette(c("grey90","yellow4","green4"))(100), 
     par.strip.text=list(cex=2.5), 
     scales=list(draw=F), 
     colorkey=list(labels=list(labels=seq(0,1,by=0.25), 
           at=seq(0,1000,by=250), 
           width=10, cex=2.5)), 
     sp.layout=list(list(country, first=FALSE), # add country borders 
         list(spatiallines, lwd=1.5, col=4))) 
# Label vertical colorkey 
grid::grid.text('Probability of occurrence', y=unit(0.5,"npc"), x=unit(0.95,"npc"), rot=90, gp=gpar(cex=3, font=2)) 

rasterstack是具有8層的RasterStack,並且在每個小區的範圍從0到1000的值,和countryspatiallines是SpatialLines *對象

我遇到過添加比例尺的各種功能,例如layout.scale.bar()maps::map.scale()scalebar(),但我無法弄清楚如何將它們合併到我使用的spplot代碼中。我已經嘗試將它們作爲參數添加爲spplot或作爲sp.layout=list()參數中的列表,但都不起作用。

謝謝!

回答

0

我推薦layout.scale.bar()layout.north.arrow(),因爲它們的輸出是SpatialPolygons。他們的佈局函數的名稱是"SpatialPolygonsRescale",您必須將其作爲列表的第一個參數。

這是我的例子;

library(sp); library(raster); library(grid) 

### example data 
r <- raster(system.file("external/test.grd", package="raster")) 
range(r) # 178400, 181600, 329400, 334000 (xmin, xmax, ymin, ymax) 
r4 <- stack(r, r, r, r) 

### preparation of scalebar etc. 
North <- list("SpatialPolygonsRescale", layout.north.arrow(type=1), 
       offset = c(178600, 333000), scale = 800, which = 1) 

North2 <- list("SpatialPolygonsRescale", layout.north.arrow(type=2), 
       offset = c(178600, 332000), scale = 1200, fill="gray", which = 2) 

scale1 <- list("SpatialPolygonsRescale", layout.scale.bar(), 
       offset = c(180500, 329800), scale = 500, fill=c("transparent","black"), which = 3) 
s1_text0 <- list("sp.text", c(180500, 329800 + 150), "0", cex = .5, which = 3) 
s1_text1 <- list("sp.text", c(180500 + 500, 329800 + 150), "500 m", cex = .5, which = 3) 

scale2.1 <- list("SpatialPolygonsRescale", layout.scale.bar(height=0.1), 
       offset = c(178600, 333000), scale = 1000, fill=c("red", "green"), which = 4) 
s2.1_text0 <- list("sp.text", c(178600, 333000 - 150), "0", cex = .5, which = 4) 
s2.1_text1 <- list("sp.text", c(178600 + 1000, 333000 - 150), "1 km", cex = .5, which = 4) 

scale2.2 <- list("SpatialPolygonsRescale", layout.scale.bar(), 
       offset = c(178600, 333200), scale = 2000, fill=c("cyan", "violet"), which = 4) 
s2.2_text0 <- list("sp.text", c(178600, 333200 + 300), "0", cex = .5, which = 4) 
s2.2_text1 <- list("sp.text", c(178600 + 2000, 333200 + 300), "2000 m", cex = .5, which = 4) 
    # Of course, you can write 180600 instead of 178600 + 2000 

### draw 
spplot(r4, layout=c(2,2), 
     sp.layout = list(North, North2, scale1, s1_text0, s1_text1, 
         scale2.1, s2.1_text0, s2.1_text1, scale2.2, s2.2_text0, s2.2_text1)) 

enter image description here

相關問題