[R-sig-Geo] Map colouring via classInt and colour Brewer

Roger Bivand Roger.Bivand at nhh.no
Wed Mar 5 13:26:33 CET 2014


On Wed, 5 Mar 2014, Alessandra Carioli wrote:

> Dear all,
>
> I have been trying to colour my maps using the same variable at 
> different points in time employing the classInt and the RColorBrewer 
> palettes. Although the matching between the map areas and data file is 
> correct (I have checked), the colouring is wrong every time. I am sure 
> the error is quite silly but I can’t understand where it lies… I have 
> tried using a custom made palette or fixed breaks, but the colouring 
> just does not work the right way! Any help on the matter would be 
> greatly appreciated!
>
> Ale
>
> Library(classInt)
> #number of class intervals
> nclassint <- 7
> # variable to be plotted
> varofint <- varT1

length(varofint)


> #define class intervals
> cat3 <- classIntervals(varofint, nclassint,style = "jenks")
> categ <- cat3
> colpal   = brewer.pal(nclassint,"RdBu")

length(rev(colpal))


> # Code for map: I want the reverse of the palette, from blue to red
> colors <- findColours(categ,rev(colpal))
> bins <- categ$brks
> lb <- length(bins)
>
>
> plot(shapefile, col=rev(colpal),axes=F)

length(varofint) == length(rev(colpal)) # FALSE, so rev(colpal) will be 
recycled,

plot(shapefile, col=rep(rev(colpal), length.out=100),axes=F)

when what you want is:

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

Because recycling may be desired, it doesn't generate a warning.

Roger


> title("Total Fertility Rate 1981", cex=1)
> legend(1096133,4844461,fill=rev(colpal),legend=paste(round(bins[-length(bins)],2)
>                                        ,"-",round(bins[-1],2)),cex=0.6, bg="white")
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
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