【问题标题】:How to add a scalebar to a single panel in spplot如何在 spplot 中将比例尺添加到单个面板
【发布时间】:2016-12-20 07:36:33
【问题描述】:

我有一组来自 RasterStack 的八张地图,并希望在绘图中添加一个 100 公里的比例尺。我在spplot 函数中使用sp.layout 在栅格顶部添加SpatialLines 对象,并假设比例尺(或指北针)也可以这样做,但我什么也做不了。

我使用的代码是:

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() 参数中,但都不起作用。

谢谢!

【问题讨论】:

    标签: r plot raster sp


    【解决方案1】:

    我推荐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))
    

    【讨论】:

      猜你喜欢
      • 2016-06-14
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多