Skip to content

how to pass panel index in spplot

5 messages · Edzer Pebesma, Weidong Gu

#
Hi,

I have a question about spplot: can it transfer somehow panel index, such as subscripts in xyplot to panels. 

The question arose when I needed to plot numbers of disease incidents on state map. The reproducible code is below. I thought panel=function () might be the solution but I couldn't get panel index in individual panels.

library(maps)
library(maptools)
state=map('state',plot=F,fill=T)
IDs=sapply(strsplit(state$names,':'), function(x) x[1])
crs=CRS('+proj=longlat +ellps=WGS84')
sp.st=map2SpatialPolygons(state, IDs, proj4string=crs)

state.inc=data.frame(State=sapply(slot(sp.st,'polygons'),function(x) slot(x,'ID')))
state.inc$D1=rpois(length(state.inc$State),100)
state.inc$D2=rpois(length(state.inc$State),150)
row.names(state.inc)=state.inc$State
inc.spdf=SpatialPolygonsDataFrame(sp.st,state.inc)

spplot(inc.spdf, zcol=2:3,
    panel = function(...) {
        sp.polygons(inc.spdf) 
        sp.text(coordinates(inc.spdf),zcol)
    }
    )

I would appreciate you help and advice.

Weidong Gu
University of Alabama, Birmingham
220A Bevill Biomedical Research Bldg
845 19th Street S., Birmingham, Alabama 35294
#
Dear Weidong,

for multi-group panel functions, the way to retrieve subscripts in
custom panel functions is

panel = function(x,y,subscripts, ...) {
 ... # do sth with subscripts here
}

see also

library(lattice)
?panel.superpose

I hope this helped,
Weidong Gu wrote:

  
    
#
Thanks Edzer for advising the option. But I could not figure out how to draw spatial polygons like state map with xyplot.

Weidong
#
Weidong, is

library(maps)
library(maptools)
state=map('state',plot=F,fill=T)
IDs=sapply(strsplit(state$names,':'), function(x) x[1])
crs=CRS('+proj=longlat +ellps=WGS84')
sp.st=map2SpatialPolygons(state, IDs, proj4string=crs)

state.inc=data.frame(State=sapply(slot(sp.st,'polygons'),function(x)
slot(x,'ID')))
state.inc$D1=rpois(length(state.inc$State),100)
state.inc$D2=rpois(length(state.inc$State),150)
row.names(state.inc)=state.inc$State
inc.spdf=SpatialPolygonsDataFrame(sp.st,state.inc)

spplot(inc.spdf, zcol=2:3,
    panel = function(x,y,z,subscripts,...) {
        panel.polygonsplot(x,y,z,subscripts,...)
        sp.text(coordinates(inc.spdf), z[subscripts])
    }
)

what you want?
Weidong Gu wrote:

  
    
#
Edzer,

Many thanks. This is just what I wanted, i.e. showing incidence numbers on the state map. 

Weidong