[R-sig-Geo] problem using levelplot() + layer() in a function

Oscar Perpiñan oscar.perpinan at upm.es
Fri Mar 21 12:45:22 CET 2014


Hello,


The key point is that "the evaluation used in 'layer' is non-standard,
and can be confusing at first: you typically refer to variables as if
inside the panel function ('x', 'y', etc); you can usually refer to
objects which exist in the global environment (workspace), but it is
safer to pass them in by name in the 'data' argument to 'layer'. (And
this should not to be confused with the 'data' argument to the
original 'xyplot'.)"

Try these three examples:

library(rasterVis)

r <- raster()
r <- init(r, runif)

foo1 <- function(r){
    rBox <- as(extent(r), 'SpatialPolygons')
    levelplot(r) +
        layer(sp.polygons(rBox, fill='black', alpha=0.2),
              data=list(rBox=rBox))
    }

foo2 <- function(r){
    levelplot(r) +
        layer({
            rBox <- as(extent(r), 'SpatialPolygons')
            sp.polygons(rBox, fill='black', alpha=0.2)
        })
}

foo3 <- function(r){
    rBox <- as(extent(r), 'SpatialPolygons')
    levelplot(r, panel=function(...){
        panel.levelplot.raster(...)
        sp.polygons(rBox, fill='black', alpha=0.2)
    })
    }


This same problem was the subject of a recent discussion in this post:
http://procomun.wordpress.com/2013/04/24/stamen-maps-with-spplot/comment-page-1/#comment-881.
There are more examples of using layer inside a function in this gist:
https://gist.github.com/oscarperpinan/7482848

Best,

Oscar.
-----------------------------------------------------------------
Oscar Perpiñán Lamigueiro
Dpto. Ingeniería Eléctrica (ETSIDI-UPM)
Grupo de Sistemas Fotovoltaicos (IES-UPM)
URL: http://oscarperpinan.github.io
Twitter: @oscarperpinan


2014-03-20 22:52 GMT+01:00 Gregory Duveiller <gduveiller at gmail.com>:
> Dear All...
>
> I am trying to make figures overlaying raster and vectors using the package rasterVis and with the functions levelplot() + layer(). All works well except when I am trying to embed those lines in a function. When I do so, only the raster images come out, not the overlaid layers. Anyone knows what I need to do? The function I have written looks like this:
>
>  makeFootprint=function(wstr,snr,FootPrint,flt,phi,scl,shf){
>
>     elps=scl*ellipse(flt)%*%matrix(c(cos(phi),-sin(phi),sin(phi),cos(phi)),nrow=2)
>     elps=elps+c(t(rep(shf[1]+wstr$cellctr$x,100)),t(rep(shf[2]+wstr$cellctr$y,100)))
>     lncoords=data.frame(x=elps[,1],y=elps[,2])
>     footprint=SpatialLines(list(Lines(list(Line(lncoords)),ID='pix')),snr at crs)
>
>     pdf(file=paste(fpath,siteName,'_window',wstr$winID,'_footprint',FootPrint,'.pdf',sep=""),width=4,height=4)
>     print(levelplot(wstr$r,col.regions=wstr$clrs,margin=FALSE,main=paste('t = ',FootPrint), colorkey = FALSE, scales=list(draw=FALSE)) +
>       layer(sp.lines(wstr$spbox, lwd=2, col='black')) +
>       layer(sp.lines(footprint, lwd=2, col='red')))
>     dev.off()
>   }
>
> The resulting PDF shows the desired raster background but a text replaces the 'layers'... mentioning "Error using packet 1 object 'wstr' not found". But the object in question is there correctly (it is actually a list with data used also to do the levelplot, which is rendered correctly).
>
> Any ideas?
>
> Is there perhaps a way to include the layers within the levelplot function directly (and avoid 'adding' them?)
>
>
> sessionInfo()
> R version 3.0.2 (2013-09-25)
> Platform: x86_64-apple-darwin10.8.0 (64-bit)
>
> locale:
> [1] C
>
> attached base packages:
> [1] grid      stats     graphics  grDevices utils     datasets  methods
> [8] base
>
> other attached packages:
> [1] ellipse_0.3-8       rasterVis_0.27      hexbin_1.26.3
> [4] latticeExtra_0.6-26 lattice_0.20-27     raster_2.2-31
> [7] rgdal_0.8-16        sp_1.0-14           RColorBrewer_1.0-5
>
> loaded via a namespace (and not attached):
> [1] zoo_1.7-11
>
>
>
> Thanks
>
> Gregory
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list