[R] the proper way to use panel functions in lattice package?

Roger Bivand Roger.Bivand at nhh.no
Tue Feb 14 09:00:53 CET 2006


On Mon, 13 Feb 2006, Deepayan Sarkar wrote:

> On 2/10/06, simon chou <sentientc at gmail.com> wrote:
> > Hi,
> > I was trying to stack a topographic map readed in from esri shapefile with a
> > contour map and a vector map. However, the plotMap(maptools) and
> > contourplot(lattice) do not seem to work well on top of each other. Here is
> > part of my code.
> >
> >
> >       xrange<-range(225000:350000)
> >
> >       yrange<-range(2685000:2810000)
> >
> >       basemap <- read.shape("Twn25_town_dxf_Polygon.shp")
> >
> >       plot.Map(basemap,xlim=xrange,ylim=yrange,fg=0,ol=8,xlab="",ylab="")
> >
> >
> >
> >       contourplot(var1.pred~x+y, spcgrid, aspect = "xy",label.style="align")
> >
> >
> >
> > I have tried to put panel function under plot.Map() but it gave some error
> > message about  some argoument matches other arguments.
> >
> >
> >
> >       >plot.Map
> > (basemap,xlim=xrange,ylim=yrange,fg=0,ol=8,xlab="",ylab="",panel=function(x,y){
> >
> >       +contourplot(var1.pred~x+y, spcgrid, aspect = "xy",label.style=
> > "align")})
> >
> >       Error in plot.default(xylims$x,xylims$y, asp=1,type="n",...):
> >
> >         argument 9 matches multiple formal arguments
> >
> >
> >
> > I also tried to put plotMap() into contourplot()'s panel but plotMap cover
> > up the conour. Maybe, there is something I miss in here.
> 
> Yes, namely that `standard graphics' (the 'graphics' package)
> functions (like plotMap) don't (easily) work with grid graphics (the
> 'grid' package) which lattice uses. Unfortunately, I have no idea if
> there are any grid compatible equivalents of plotMap.

In `standard graphics' this is not a problem, and can be built up by just 
by using the add=TRUE argument (using contour). You may find that, with 
many polygon boundaries and contours, you need to set line widths and/or 
colours to differentiate what is representing what.

The lattice contourplot function also fills the inter-contour surfaces, so 
you would need to draw the polygon boundaries on top of the filled colour. 
As you can see from the example in the spatial graph gallery:

http://r-spatial.sourceforge.net/gallery/#fig20.R

this is far from easy, but with determination can be done (recall that 
lattice graphics are excellent for multiple plots conditioning on other 
variables, which is not the case here as far as you have explained). The 
example in the graph gallery is for kriged output, by the way.

Using the maptools package and sp classes (see R-News), I would say:

library(maptools)
col <- readShapePoly(system.file("shapes/columbus.shp", package="maptools")[1])
plot(col, border="grey")
library(akima) # to make a grid
crds <- coordinates(col)
contour(interp(crds[,1], crds[,2], col$CRIME), add=TRUE)

or for an image:

image(interp(crds[,1], crds[,2], col$CRIME), axes=FALSE)
plot(col, border="grey35", add=TRUE)

For these kinds of questions, the Task View (left panel on CRAN) called 
"Spatial" and the R-sig-geo mailing list might have helped.

> 
> > What went wrong there? Also, is there any diffeence between
> > contourplot(lattice) and counterLine(base)? These 2 functions seem to give
> > difference contour from the same data set. contourplot(lattice) seem to give
> > better looking contour than contourLine() or contour().
> 
> contourplot uses contourLines (which I assume is what you meant)
> internally, so any differences probably stem from the choice of
> default arguments.
> 
> >
> > ps. I krige 1700+ simulated observations into 4000+ regular spaced data to
> > get contour.
> 
> Deepayan
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list