[R-sig-Geo] How does col.regions work in spplot?

Ken Takagi katakagi at bu.edu
Tue Sep 18 21:02:25 CEST 2012


Hi,

I am trying to use the same colors for polygon borders and fill in a spplot
figure.  I can use the "col.regions" attribute to have spplot fill the polygons
according to a color ramp.  What I would like is to have spplot also color the
borders of the polygons using the exact same color as the fill.  Here is an
example where I fill the polygons using col.regions = heat.colors(100), but
leave borders black:

# read in data
library(maptools)
library(sp)
nc <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], 
                    proj4string=CRS("+proj=longlat +datum=NAD27"))
# add data column of random numbers
nc at data$rand_data <- rnorm(nrow(nc at data), 0.8, 0.3)
# Plot data with black polygons border
spplot(nc, zcol = "rand_data", col.regions = heat.colors(100), colorkey = T)

The only way I have found to match the color of the polygon borders (so far...)
has been to use a for loop inside spplot (probably a bad thing!!) and use the
"zcol" data as an index to select the appropriate heat.color() to match the fill:

# rank rand_data between 1 and 100 to match length of heat.colors(100), so
# they can be used as index for heat.colors(100)
rank_county_data = rank(nc at data$rand_data)
# Plot data with colored borders using for loop
spplot(nc, zcol = "rand_data", col.regions = heat.colors(100), colorkey = T,
             panel = function(x, y, ...){
               panel.polygonsplot(x,y, ...) 
               for(i in 1:length(fips)){
                 sub_county = subset(nc, nc at data$FIPS %in% nc at data$FIPS[i])
                 sp.polygons(sub_county, col = 
                            heat.colors(100)[rank_county_data[i]])
               }
             }) 

As you can see, the color of the borders are close, but not exactly the same as
the fill colors.  How does col.regions decide on the fill color (I assumed it
does something like what I did).  Is there a better way to match up the color of
the border and the color of the fill that doesn't involve a loop?

Thanks in advance!!!

Ken



More information about the R-sig-Geo mailing list