Skip to content
Prev 20518 / 29559 Next

Map colouring via classInt and colour Brewer

On Wed, 5 Mar 2014, Roger Bivand wrote:

            
This should be:

plot(shapefile, col=rep(rev(colpal), length.out=length(varofint)), ...

I was using North Carolina to replicate with - I wanted to demonstrate the 
consequences of unintended recycling in a reproducible way. The top of the 
code was:

library(maptools)
shapefile <- readShapeSpatial(system.file("shapes/sids.shp",
  package="maptools")[1], IDvar="FIPSNO",
  proj4string=CRS("+proj=longlat +ellps=clrk66"))
varofint <- shapefile$BIR74
library(classInt)
nclassint <- 7
cat3 <- classIntervals(varofint, nclassint, style = "jenks")
categ <- cat3
library(RColorBrewer)
colpal   = brewer.pal(nclassint, "RdBu")
colors <- findColours(categ, rev(colpal))
opar <- par(mfrow=c(2,1))
plot(shapefile, col=rev(colpal), axes=FALSE)
plot(shapefile, col=rep(rev(colpal), length.out=length(varofint)),
  axes=FALSE)
par(opar)

but the intention was:

plot(shapefile, col=colors, axes=FALSE)

Roger