Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在rasterVis中打印具有重叠多边形的光栅时的范围问题_R_Plot_Scoping_Sp_Sf - Fatal编程技术网

在rasterVis中打印具有重叠多边形的光栅时的范围问题

在rasterVis中打印具有重叠多边形的光栅时的范围问题,r,plot,scoping,sp,sf,R,Plot,Scoping,Sp,Sf,我正在开发一个基于rasterVis::levelplot的绘图功能,用户可以将光栅对象或光栅对象和sf多边形对象传递到该功能 函数相当复杂,但显示问题的最小子集如下所示: library(sf) library(raster) library(rasterVis) myplot <- function(in_rast, in_poly = NULL) { rastplot <- rasterVis::levelplot(in_rast, margin = FALSE) p

我正在开发一个基于
rasterVis::levelplot
的绘图功能,用户可以将光栅对象或光栅对象和
sf
多边形对象传递到该功能

函数相当复杂,但显示问题的最小子集如下所示:

library(sf)
library(raster)
library(rasterVis)

myplot <- function(in_rast, in_poly = NULL) {
  rastplot <- rasterVis::levelplot(in_rast, margin = FALSE)
  polyplot <- layer(sp::sp.polygons(in_poly))
  print(rastplot + polyplot)
}
并对其功能进行了测试。理论上,我认为这应该是可行的:

my_poly  <- as(my_poly, "Spatial") # convert to spatial
myplot(in_rast, in_poly = my_poly)

有人能解释一下这里发生了什么吗?这显然是(?)一个范围问题,但我真的不明白为什么函数的行为是这样的


提前谢谢

latticeExtra::layer的帮助页面说明:

层中使用的评估是非标准的,一开始可能会让人困惑:您通常引用变量,就像在面板函数中一样(x、y等);您通常可以引用存在于全局环境(工作区)中的对象,但在数据参数中将它们按名称传递给图层更安全

在函数中使用
时,可以将对象嵌入列表中,并将其传递到
数据
参数中:

myplot <- function(in_rast, in_poly = NULL) {
  rastplot <- levelplot(in_rast, margin = FALSE)
  polyplot <- layer(sp.polygons(x), 
                    data = list(x = in_poly))
  print(rastplot + polyplot)
}

in_poly <- my_poly
in_poly <- as(in_poly, "Spatial")
myplot(in_rast, in_poly = in_poly)
in_poly <- structure(list(cell_id = 1:4, geometry = structure(list(structure(list(
    structure(c(0, 0.5, 0.5, 0, 0, 0, 0, 0.5, 0.5, 0), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(0.5, 1, 1, 0.5, 0.5, 0, 0, 0.5, 0.5, 0), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(0, 0.5, 0.5, 0, 0, 0.5, 0.5, 1, 1, 0.5), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg")), structure(list(
    structure(c(0.5, 1, 1, 0.5, 0.5, 0.5, 0.5, 1, 1, 0.5), .Dim = c(5L, 
    2L))), class = c("XY", "POLYGON", "sfg"))), n_empty = 0L, class = c("sfc_POLYGON", 
"sfc"), precision = 0, crs = structure(list(epsg = NA_integer_, 
    proj4string = NA_character_), .Names = c("epsg", "proj4string"
), class = "crs"), bbox = structure(c(0, 0, 1, 1), .Names = c("xmin", 
"ymin", "xmax", "ymax")))), .Names = c("cell_id", "geometry"), row.names = c(NA, 
4L), class = c("sf", "data.frame"), sf_column = "geometry", 
agr = structure(NA_integer_, class = "factor", .Label = c("constant", 
"aggregate", "identity"), .Names = "cell_id"))


in_poly  <- as(in_poly, "Spatial")
myplot(in_rast, in_poly = in_poly)
myplot <- function(in_rast, in_poly = NULL) {
  rastplot <- levelplot(in_rast, margin = FALSE)
  polyplot <- layer(sp.polygons(x), 
                    data = list(x = in_poly))
  print(rastplot + polyplot)
}
myplot(in_rast, in_poly = my_poly)