# [R-sig-Geo] drawing order in spplot

Duncan Golicher dgoliche at sclc.ecosur.mx
Thu Jul 6 17:52:05 CEST 2006

```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.

#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))

#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:

> I stumbled there too.
> A way is to customize the default panel function within spplot.
>
> Try:
>
> spplot(municipios, "yourvariable", panel <- function(x,y,...){
>     sp.grid(chisgrid)
>     panel.polygonsplot(x,y,...)
>     })
>
> Regards, Arien
>
> Duncan Golicher schreef:
>
>> Many thanks to everyone involved in putting together the excelent sp
>> package. I have just used for teaching a brief intro to applied
>> geostats with gstat. Students found life much easier than a year ago
>> when I had to try to explain a less structured approach to the data.
>> A simple question arose after the class. According to the spplot
>> documentation "for grids and polygons sp.layout is drawn afterwards
>> (so the item will not be overdrawn by the grid and/or polygon)".
>> So in this example we expected a DEM to be drawn first, then
>> municipal boundaries.
>>
>>  > class(municipios)
>> [1] "SpatialPolygonsDataFrame"
>>  > class(chisgrid)
>> [1] "SpatialPixelsDataFrame"
>>
>> municips <- list("sp.polygons", municipios)
>> spplot(chisgrid["dem"],sp.layout=municips)
>>
>> The municipal boundaries are drawn first then overdrawn by the DEM,
>> which was not what we expected or wanted. I couldn't find how to
>> avoid this, apart from the simple solution of ..
>>
>> image(chisgrid,col=terrain.colors(20))
>>
>> instead of spplot.The figures on  http://r-spatial.sourceforge.net
>> all appear to have the grid drawn first. Is there a way to reverse this?
>>
>> Duncan
>>
>

--
Dr Duncan Golicher
Ecologia y Sistematica Terrestre
El Colegio de la Frontera Sur
San Cristobal de Las Casas,
Chiapas, Mexico

Email: dgoliche at sclc.ecosur.mx

Tel: 967 674 9000 ext 1310
Fax: 967 678 2322
Celular: 044 9671041021

United Kingdom Skypein; 020 7870 6251
Skype name: duncangolicher