在sp图的不同面板中绘制轮廓

时间:2018-07-27 16:35:40

标签: r raster contour lattice sp

我在堆栈中有3个不同的栅格。我需要绘制一个面板图,并在每个面板上添加不同的shapefile。到目前为止,我已经做到以下几点;

    ## read the libraries
    library(raster)
    library(rgdal)
    library(sp)
    library(rworldmap)
    library(OceanView)

    ##random raster object
    r <- raster(ncol=40, nrow=20)
    r[] <- rnorm(n=ncell(r))
    # Create a RasterStack object with 3 layers
    s <- stack(x=c(r, r*2, r**2))

    ##coordinate system
    wgs<-CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")


    ##reading the additional shape files
    w <- spTransform(getMap(), wgs)
    poly <- list(list("sp.lines", as(w, 'SpatialLines'), lwd = 
            0.5,col="black"))

    ##plotting with spplot
    plot(spplot(s,layout=c(3,1),sp.layout=poly,
                 colorkey =list(space = "right"),
                names.attr = c("a","b","c")))

到目前为止,我已经绘制了3个栅格,上面覆盖了shapefile。现在,我需要在面板图上绘制三个不同的轮廓。并且还需要在每个图上绘制风速箭头。我知道要做到这一点,我需要使用Contour()和quiver()函数。但是,我无法绘制这些图。

    ## different raster stack for the contour plot
    s1 <- stack(x=c(r/2, r*10, r**5))


    ##differnt wind components
    lat= matrix(rep(seq(-90,90,length.out=20),each=20), ncol=20, byrow=TRUE)
    lon=matrix(rep(seq(-180,180,length.out=20),each=20), ncol=20, byrow=F)
    u=matrix(rep(sample(seq(-2,2,length.out=1000),20),each=20), ncol=20, byrow=TRUE)
    v=matrix(rep(sample(seq(-2,2,length.out=1000),20),each=20), ncol=20, byrow=TRUE)

    ##plot the arrows
    quiver2D(u = u,v=v,x = lon, y = lat,add=T,type="simple")

有人可以帮助我吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

首先,您需要确定晶格范式中的哪个函数被调用。这需要要么查看“ spplot”帮助页面(我先是失败了),要么遵循调用树中的类和函数(这是我实际上所做的)。然后查看最终功能的帮助页面,查看是否可以传递参数来添加轮廓线:

showMethods("spplot",class="Raster", includeDefs=TRUE) # last argument need to see code
Function: spplot (package sp)
obj="Raster"
function (obj, ...) 
{
    .local <- function (obj, ..., maxpixels = 50000, as.table = TRUE, 
        zlim) 
    {
        obj <- sampleRegular(obj, maxpixels, asRaster = TRUE, 
            useGDAL = TRUE)
        if (!missing(zlim)) {
            if (length(zlim) != 2) {
                warning("zlim should be a vector of two elements")
            }
            if (length(zlim) >= 2) {
                obj[obj < zlim[1] | obj > zlim[2]] <- NA
            }
        }
        obj <- as(obj, "SpatialGridDataFrame")
        spplot(obj, ..., as.table = as.table)
    }
    .local(obj, ...)
}

> showMethods("spplot",class="SpatialGridDataFrame",includeDefs=TRUE)
Function: spplot (package sp)
obj="SpatialGridDataFrame"
function (obj, ...) 
spplot.grid(as(obj, "SpatialPixelsDataFrame"), ...)

> showMethods("spplot.grid",class="SpatialPixelsDataFrame",includeDefs=TRUE)

Function "spplot.grid":
 <not an S4 generic function>
> spplot.grid
Error: object 'spplot.grid' not found
> getAnywhere(spplot.grid)
A single object matching ‘spplot.grid’ was found
It was found in the following places
  namespace:sp
with value

function (obj, zcol = names(obj), ..., names.attr, scales = list(draw = FALSE), 
    xlab = NULL, ylab = NULL, aspect = mapasp(obj, xlim, ylim), 
    panel = panel.gridplot, sp.layout = NULL, formula, xlim = bbox(obj)[1, 
        ], ylim = bbox(obj)[2, ], checkEmptyRC = TRUE, col.regions = get_col_regions()) 
{
    if (is.null(zcol)) 
        stop("no names method for object")
    if (checkEmptyRC) 
        sdf = addNAemptyRowsCols(obj)
    else sdf = as(obj, "SpatialPointsDataFrame")
    if (missing(formula)) 
        formula = getFormulaLevelplot(sdf, zcol)
    if (length(zcol) > 1) {
        sdf = spmap.to.lev(sdf, zcol = zcol, names.attr = names.attr)
        zcol2 = "z"
    }
    else zcol2 = zcol
    if (exists("panel.levelplot.raster")) {
        opan <- lattice.options("panel.levelplot")[[1]]
        lattice.options(panel.levelplot = "panel.levelplot.raster")
    }
    scales = longlat.scales(obj, scales, xlim, ylim)
    args = append(list(formula, data = as(sdf, "data.frame"), 
        aspect = aspect, panel = panel, xlab = xlab, ylab = ylab, 
        scales = scales, sp.layout = sp.layout, xlim = xlim, 
        ylim = ylim, col.regions = col.regions), list(...))
    if (all(unlist(lapply(obj@data[zcol], is.factor)))) {
        args$data[[zcol2]] = as.numeric(args$data[[zcol2]])
        if (is.null(args$colorkey) || (is.logical(args$colorkey) && 
            args$colorkey) || (is.list(args$colorkey) && is.null(args$colorkey$at) && 
            is.null(args$colorkey$labels))) {
            if (!is.list(args$colorkey)) 
                args$colorkey = list()
            ck = args$colorkey
            args$colorkey = NULL
            args = append(args, colorkey.factor(obj[[zcol[1]]], 
                ck))
        }
        else args = append(args, colorkey.factor(obj[[zcol[1]]], 
            ck, FALSE))
    }
    ret = do.call(levelplot, args)
    if (exists("panel.levelplot.raster")) 
        lattice.options(panel.levelplot = opan)
    ret
}
<bytecode: 0x7fae5e6b7878>
<environment: namespace:sp>

您可以看到它支持通过, list(...))传递的其他参数。发生这种情况因此很容易将轮廓线添加到带有levelplot的{​​{1}}上,尽管它只出现在“参数”列表中,而不出现在“ contour=TRUE”的“用法”部分的命名参数中。尽管如此,在levelplot页上的示例中进行的测试表明它成功了。您的示例并不是一个特别好的示例,因为它的粒度非常细,并且没有上升或下降的模式。但是,将?levelplot添加到contour=TRUE,的参数中会产生黑色轮廓线。 (时间戳记是由于我的Rprofile设置中的晶格代码,所以不会显示在您的设备上。)

spplot

enter image description here

如果有人到处入侵 png(); plot(spplot(s,layout=c(3,1),sp.layout=poly, contour=TRUE, colorkey =list(space = "right"), names.attr = c("a","b","c"))) ; dev.off() 或也许spplot.grid,那么levelplot作者的材料可能有用: https://markmail.org/search/?q=list%3Aorg.r-project.r-help+lattice+add+contours+to+levelplot#query:list%3Aorg.r-project.r-help%20lattice%20add%20contours%20to%20levelplot%20from%3A%22Deepayan%20Sarkar%22+page:1+mid:w7q4l7dh6op2lfmt+state:results

相关问题