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

Edzer Pebesma edzer.pebesma at uni-muenster.de
Tue Aug 12 22:23:46 CEST 2008


After getting the data from Jaime offline, I got this working, following 
Roger's suggestion.

Code is on cvs and will be in the next release.
--
Edzer

Roger Bivand wrote:
> 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
>>
>

-- 
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/




More information about the R-sig-Geo mailing list