[GIS] RasterVis levelplot in R with scale bar

levelplotrraster

I'm using the rasterVIS package in R to create maps with levelplot, which works fine. But I cannot figure out how to add a spatial scale bar to the maps. Does anyone know?

Best Answer

There is a code example for adding a spatial scalebar in the FAQ section of rasterVis:

https://oscarperpinan.github.io/rastervis/FAQ.html#scalebar

f <- system.file("external/test.grd", package="raster")
r <- raster(f)

levelplot(r) +
  layer({
    xs <- seq(181000, 181400, by=100)
    grid.rect(x=xs, y=330500,
                  width=100, height=30,
                  gp=gpar(fill=rep(c('transparent', 'black'), 2)),
                  default.units='native')
    grid.text(x= xs - 50, y=330560, seq(0, 400, by=100),
              gp=gpar(cex=0.5), rot=30,
              default.units='native')
    })