在光栅*对象的图中,轴最小程度,无填充

Ben*_*min 19 r

有没有办法确保绘图周围的框与光栅范围完全匹配?在下面,栅格的上方和下方或左侧和右侧有一个间隙,具体取决于设备比例:

require(raster)
r = raster()
r[]= 1
plot(r, xlim=c(xmin(r), xmax(r)), ylim=c(ymin(r), ymax(r)))
Run Code Online (Sandbox Code Playgroud)

栅格对象问题的一个要素是asp=1确保正确显示.以下基本散点图在以下情况下具有相同的问题asp=1:

plot(c(1:10), c(1:10), asp=1)
Run Code Online (Sandbox Code Playgroud)

尝试vectorplot(r)从rasterVis包中查看我想要的轴的样子.

编辑:

解决方案需要与SpatialPoints叠加层配合使用,而不是显示指定栅格限制之外的点:

require(raster)
require(maptools)

# Raster
r = raster()
r[]= 1

# Spatial points
x = c(-100, 0, 100)
y = c(100, 0, 100)
points = SpatialPoints(data.frame(x,y))

plot(r, xlim=c(xmin(r), xmax(r)), ylim=c(ymin(r), ymax(r)))
plot(points, add=T)
Run Code Online (Sandbox Code Playgroud)

Jos*_*ien 13

您可能最好使用其中一个lattice基于函数来绘制由包rasterrasterVis包提供的空间栅格对象.你发现了他们中的一个vectorplot(),但spplot()还是levelplot()最好在这种情况下满足您的需求.

(base graphics基于对象的对象plot()方法"RasterLayer"不允许任何简单的方法来设置具有适当宽高比的轴.对于任何有兴趣的人,我会在帖子底部的部分详细介绍为什么会这样.)

作为levelplot()产生的情节的一个例子:

require(raster)
require(rasterVis)

## Create a raster and a SpatialPoints object.
r <- raster()
r[] <- 1:ncell(r)
SP <- spsample(Spatial(bbox=bbox(r)), 10, type="random")

## Then plot them    
levelplot(r, col.regions = rev(terrain.colors(255)), cuts=254, margin=FALSE) +
layer(sp.points(SP, col = "red"))

## Or use this, which produces the same plot.
# spplot(r, scales = list(draw=TRUE), 
#        col.regions = rev(terrain.colors(255)), cuts=254) +
# layer(sp.points(SP, col = "red"))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

这些方法中的任何一个仍然可以绘制符号的某些部分,该部分表示落在绘制的栅格之外的点.如果您想避免这种可能性,您可以只对您的SpatialPoints对象进行子集化,以删除任何落在栅格之外的点.这是一个简单的功能,它将为您做到这一点:

## A function to test whether points fall within a raster's extent
inExtent <- function(SP_obj, r_obj) {
    crds <- SP_obj@coord
    ext  <- extent(r_obj)
    crds[,1] >= ext@xmin  &  crds[,1] <= ext@xmax &
    crds[,2] >= ext@ymin  &  crds[,2] <= ext@ymax
}
## Remove any points in SP that don't fall within the extent of the raster 'r'
SP <- SP[inExtent(SP, r), ]
Run Code Online (Sandbox Code Playgroud)

额外的杂乱细节,关于为什么它很难plot(r)制作紧密贴合的轴

plot被称为类型的对象上raster,该光栅数据是(最终),使用任一绘制rasterImage()image().遵循哪条路径取决于:(a)被绘制的设备类型; (b)useRaster原始plot()电话中的参数值.

在任何一种情况下,绘图区域的设置方式都会产生填充绘图区域的轴,而不是以赋予它们适当宽高比的方式.

下面,我将展示在执行此步骤的过程中调用的函数链,以及最终设置绘图区域的调用.在这两种情况下,似乎没有简单的方法来改变绘制的轴的范围和纵横比.

  • useRaster=TRUE

    ## Chain of functions dispatched by `plot(r, useRaster=TRUE)`
    getMethod("plot", c("RasterLayer", "missing"))
    raster:::.plotraster2
    raster:::.rasterImagePlot
    
    ## Call within .rasterImagePlot() that sets up the plotting region
    plot(NA, NA, xlim = e[1:2], ylim = e[3:4], type = "n",
               , xaxs = "i", yaxs = "i", asp = asp, ...)
    
    ## Example showing why the above call produces the 'wrong' y-axis limits
    plot(c(-180,180), c(-90,90), 
         xlim = c(-180,180), ylim = c(-90,90), pch = 16,
         asp = 1,
         main = "plot(r, useRaster=TRUE) -> \nincorrect y-axis limits")
    
    Run Code Online (Sandbox Code Playgroud)
  • useRaster=FALSE

    ## Chain of functions dispatched by `plot(r, useRaster=FALSE)`
    getMethod("plot", c("RasterLayer", "missing"))
    raster:::.plotraster2
    raster:::.imageplot
    image.default
    
    ## Call within image.default() that sets up the plotting region
    plot(NA, NA, xlim = xlim, ylim = ylim, type = "n", xaxs = xaxs, 
         yaxs = yaxs, xlab = xlab, ylab = ylab, ...)
    
    ## Example showing that the above call produces the wrong aspect ratio
    plot(c(-180,180), c(-90,90), 
         xlim = c(-180,180), ylim = c(-90,90), pch = 16,
         main = "plot(r,useRaster=FALSE) -> \nincorrect aspect ratio")
    
    Run Code Online (Sandbox Code Playgroud)