[R-sig-Geo] specifying different colors for vector map as background

Roger Bivand Roger.Bivand at nhh.no
Thu Jul 31 22:17:12 CEST 2008


On Thu, 31 Jul 2008, Jaime Carrera wrote:

> Hi,
> I'm trying to create a spplot with a vector map on the background. It works 
> fine if I don't want the polygon map to be filled, but what I do want to is 
> to have the polygons filled with different colors. The following command 
> plots the polygon map fine, filling each polygon with its respective color:
>
> plot(avi,axes=FALSE,lty=0,col=rgb(avi$r/255,avi$g/255,avi$b/255))
>
> Now I try to do this on ssplot, but it doesn't work, as all polygons are 
> filled with the first color found:
>
> basemap<-list("sp.polygons",avi,fill=rgb(avi$r/255,avi$g/255,avi$b/255),lty=1)
> spplot(wells2001sp,c("May","Jun"),as.table=TRUE,sp.layout=list(basemap))
>
> Is there a way around it?

Not for spplot in the current code. The fill= argument in "sp.polygons" is 
passed through to the internal function sp.polygon3 without being indexed, 
so grid.polygon gets given the whole fill vector each time, and takes the 
first value. We can try to add the functionality at some time in the 
future - or others could help - type sp.polygons at the prompt to see the 
code. fill= is being passed in the ... arguments, and needs to be 
conditioned on, and fill=fill[i] passed to sp.polygon3.

The sp code is on the r-spatial sourceforge site - patches welcome!

Roger

PS: This is a first cut:

sp.polygons = function(obj, col = 1, fill="transparent", ...) {
 	sp.polygon3 = function(x, col, fill, ...) {
 		cc = slot(x, "coords")
 		grid.polygon(cc[,1], cc[,2], default.units = "native",
 			gp = gpar(col = col, fill = fill, ...))
 		panel.lines(cc, col = col, ...)
 	}
 	if (is.character(obj))
 		obj = get(obj)
 	if (!is(obj, "SpatialPolygons"))
 		stop(paste("object extending class SpatialPolygons",
 		    "expected; got class", class(obj)))
 	else
 		obj = as(obj, "SpatialPolygons")
 	pls = slot(obj, "polygons")
    	pO <- slot(obj, "plotOrder")
 	if (length(fill) != length(pO)) fill <- rep(fill[1], length(pO))
    	for (i in pO) {
    		Srs <- slot(pls[[i]], "Polygons")
    		pOi <- slot(pls[[i]], "plotOrder")
    		for (j in pOi)
 		    sp.polygon3(Srs[[j]], col = col, fill = fill[i], ...)
 	}
}




>
> Thanks,
>
> Jaime
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
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-sig-Geo mailing list