[R-sig-Geo] round class intervals in legend

Roger Bivand Roger.Bivand at nhh.no
Tue Jul 17 15:21:23 CEST 2007


On Tue, 3 Jul 2007, Marco Helbich wrote:

> Dear list,
>
> I've analysed my dataset with the fanny() cluster analysis function and now I want to visualise the membership values. My question is: how can I round the class interval values printed in the legend (e.g. 3 digits)? The round() function doesn't work within the classIntervals().
>
> Below you can find an fictitious example with the sids dataset:
>
> library(cluster)
>
> xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1], IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))
> plot(xx, border="blue", axes=TRUE, las=1)
> names(xx)
> dat <- cbind(xx$SID74, xx$NWBIR74, xx$BIR79)
> fann <- fanny(dat, 3, maxit = 1000)
>
> par(mar=c(0.5,0.5,0.5,0.5))
> plotvar <- fann$membership[,1]
> nclr <- 6
> plotclr <- brewer.pal(nclr, "BuPu")
> class <- classIntervals(plotvar, nclr, style = "quantile")
> colcode <- findColours(class, plotclr)
> plot(xx)
> plot(xx, col = colcode, add = T)
> legend("topleft", legend=names(attr(colcode, "table")),
>    fill=attr(colcode, "palette"), cex = 0.75, bty = "n")
>
> I appreciate every hint! Thanks.

Sorry for the long delay, and thanks for the clear example. Use the 
"fixed" style to insert the fixed breaks - rounding the legend might be 
misleading, since some objects might change class:

brks <- round(class$brks, 3)
brks
class <- classIntervals(plotvar, nclr, style = "fixed",
   fixedBreaks = brks)
colcode <- findColours(class, plotclr)
plot(xx, col = colcode)
legend("topleft", legend=names(attr(colcode, "table")),
   fill=attr(colcode, "palette"), cex = 0.75, bty = "n")

looks OK, but beware of the largest and smallest breaks "orphaning" data 
at the edges through rounding.

Hope this helps,

Roger


>
> Marco
> __________________
>
> Marco Helbich
> ISR
> Postgasse 7/4/2
> A-1010, Vienna
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, 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