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

Gregory Duveiller gduveiller at gmail.com
Thu Mar 20 22:52:11 CET 2014


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 


More information about the R-sig-Geo mailing list