Skip to content

drawing order in spplot

5 messages · Duncan Golicher, Arien Lam, Edzer Pebesma

#
Thanks for this Arien,

Just can't get it to work though. I messed around a bit more yesterday 
but couldn't find a way. No one else on the list replied. I have another 
reason to want to do something similar, so I was hoping there was a work 
around out there. Here is the data if you (or anyone else) can help.

load(file=url("ftp://200.23.34.16/simulacion/SPATIAL/chisgrid.rob"))
load(file=url("ftp://200.23.34.16/simulacion/SPATIAL//municipios.rob"))

#municipios was originally imported from a shapefile. The "dem" is a 
coarse 1 km x 1km grid imported from GRASS 5.7. Both use UTM NAD27, but 
the projections are not #set. The data represents the state of Chiapas 
in Southern Mexico

image(chisgrid,"dem",col=terrain.colors(100))
plot(municipios,add=T)

#Gives what we wanted in base graphics

#But
 
spplot(municipios, "AREA", panel <- function(x,y,...){
    sp.grid(chisgrid)
    panel.polygonsplot(x,y,...)
    })
#Doesn't

#and

municips <- list("sp.polygons", municipios)
spplot(chisgrid["dem"],sp.layout=municips)

#Draws them in the wrong order.

#As an aside, this is great fun with rgl and the students loved it. Can 
anyone improve the code?

view3d<-function(grid=chisgrid,dem=1,drape=2,exag=3,pal=sp.theme()$regions$col){
  fullgrid(grid)<-TRUE
  y<-grid[[dem]]*exag
  y[is.na(y)]<-0
  dim(y)<-grid at grid@cells.dim

  x <- grid at grid@cellsize[1]*(1:grid at grid@cells.dim[1])
  z <-grid at grid@cellsize[2]*(1:grid at grid@cells.dim[2])

  y2<-grid[[drape]]
  y2[!is.na(y2)]<-as.numeric(cut( y2[!is.na(y2)],length(pal)))
  y2[is.na(y2)]<-0

  ylim <- range(y2)
  ylen <- ylim[2] - ylim[1] + 1

  pal<-c("black",pal)
  col <- pal[ y2-ylim[1]+1 ] # assign colors to heights for each point

  rgl.clear()
  rgl.surface(x, z, y, color=col)
}

view3d(drape=1,pal=terrain.colors(100))

Duncan
Arien Lam wrote:

            

  
    
#
Duncan,

After a few tries I found:

spplot(chisgrid, "dem", panel = function(x,y, ...){
    panel.gridplot(x,y, ...)
    sp.polygons(municipios, col=1,fill=0)
    })

, which is just the reverse nesting order from my previous shot at it.
HTH, Arien

To be precise, this worked on:
Version 2.3.1 (2006-06-01)
i486-pc-linux-gnu

attached base packages:
[1] "methods"   "stats"     "graphics"  "grDevices" "utils"     "datasets"
[7] "base"

other attached packages:
 lattice       sp
"0.13-8" "0.8-16"


This approach is advocated for tweaking trellis plots.
On Thu, July 6, 2006 17:52, Duncan Golicher wrote:

  
    
#
Great. Thanks so much. It was obvious really.
You also seem to have come up with one solution to the line width 
question asked a couple of days ago,  as this works.

spplot(chisgrid, "dem", panel = function(x,y, ...){
    panel.gridplot(x,y,...)
    sp.polygons(municipios, col=1,fill=0,lwd=3,lty=2)
    })


Duncan
Arien Lam wrote:

            

  
    
3 days later
#
I uploaded sp 0.8-18 to CRAN, which should fix:
- a bug that breaks sp 0.8-16 on r-devel
- not passing lty and lwd to spplot methods for SpatialPolygonsDataFrame
and SpatialLinesDataFrame objects.
--
Edzer
Duncan Golicher wrote:
#
Duncan Golicher wrote:
Yes, or:

spplot(chisgrid, "dem", sp.layout=list("sp.polygons", municipios, col=1, 
fill=0, lwd=3, lty=2, first=F))