Skip to content
Prev 19404 / 29559 Next

plotting polygons of one shapefile in different colors?

On Thu, 26 Sep 2013, cyndy-jer wrote:

            
The easier way is to get to know and like factors, categorical variables 
typically with few levels:

library(sp)
data(meuse.grid)
coordinates(meuse.grid) <- c("x", "y")
gridded(meuse.grid) <- TRUE
class(meuse.grid$ffreq)
library(RColorBrewer)
spplot(meuse.grid, "ffreq1",
  col.regions=brewer.pal(nlevels(meuse.grid$ffreq), "Accent"))

but if the plotted variable is numeric (or integer):

meuse.grid$ffreq1 <- as.integer(meuse.grid$ffreq)
spplot(meuse.grid, "ffreq1", 
col.regions=brewer.pal(nlevels(meuse.grid$ffreq), "Accent"))

you get trouble because at= by default splits the range of the variable 
into 20, cycling the colours. You can fix at=

spplot(meuse.grid, "ffreq1", at=seq(0.5, 3.5, 1),
  col.regions=brewer.pal(nlevels(meuse.grid$ffreq), "Accent"))


but if your variable is really categorical, convert it to factor 
representation, and live gets easier.

Roger