[R-sig-Geo] how to pass panel index in spplot

Edzer Pebesma edzer.pebesma at uni-muenster.de
Wed Apr 7 23:00:47 CEST 2010


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:
> Thanks Edzer for advising the option. But I could not figure out how to draw spatial polygons like state map with xyplot.
> 
> Weidong
> 
>> -----Original Message-----
>> From: Edzer Pebesma [mailto:edzer.pebesma at uni-muenster.de]
>> Sent: Wednesday, April 07, 2010 3:09 PM
>> To: Weidong Gu
>> Cc: 'r-sig-geo at stat.math.ethz.ch'
>> Subject: Re: [R-sig-Geo] how to pass panel index in spplot
>>
>> 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:
>>> 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
>>>
>>> _______________________________________________
>>> R-sig-Geo mailing list
>>> R-sig-Geo at stat.math.ethz.ch
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>> --
>> Edzer Pebesma
>> Institute for Geoinformatics (ifgi), University of Münster
>> Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
>> 8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
>> http://www.52north.org/geostatistics      e.pebesma at wwu.de

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list