[R-sig-Geo] Map legend
Roger Bivand
Roger.Bivand at nhh.no
Thu Feb 16 13:18:45 CET 2006
On Thu, 16 Feb 2006, Mihai Nica wrote:
> Greetings:
>
> I am unable to change the color to some sort of a gray scale in a map. Also,
> I have a hard time adding a meaningful legend to it. These must be for
> dummies question. Looked it up everywhere, but could not find a solution
> (not even a similar question). The map is:
>
>
> a=read.shape("Density.shp", dbf.data=TRUE, verbose=TRUE)
> plot.Map(a, auxvar=pidg$AVG_IGR, xlab="AVG_IGR quintiles", ylab=" ",ol="NA",
> color="blue", nclass=4)
>
> if I change:
>
> color=gray or black
>
> Error in hsv(rep(color.list$hsvcol[cind], nclass), c(pr, 1)) :
> bad hsv to rgb color conversion
It is much easier to use plot() methods for SpatialPolygon objects, and
those methods will be maintained, but plot.Map() isn't going anywhere -
it tries to do too much with the wrong kinds of objects.
The help page even explains that you can pass a vector of arbitrary
colours through the fg= argument. The reason for the color argument not
working is that only c("blue", "green", "yellow", "red") are allowed in
its internal color.ramp() function - it is hardcoded, and unnecessary now
that alternatives are available.
Here are the problems:
library(maptools)
x <- read.shape(system.file("shapes/sids.shp", package="maptools")[1])
plot.Map(x, auxvar=x$att.data$BIR74, ol=NA, color="blue", nclass=4)
plot.Map(x, auxvar=x$att.data$BIR74, ol=NA, color="grey", nclass=4)
because "grey" isn't in the hardwired list. But using the wonderful
colorRampPalette() function, rolling your own colours is easy, here 100
for one per county:
grays <- colorRampPalette(c("grey90", "grey10"))(100)
plot.Map(x, fg=grays[rank(x$att.data$BIR74)], ol=NA)
or four classes:
moregrays <- colorRampPalette(c("grey90", "grey10"))(4)
brks <- quantile(x$att.data$BIR74)
plot.Map(x, fg=moregrays[findInterval(x$att.data$BIR74, brks,
all.inside=TRUE)], ol=NA)
All of the maintained plot() methods - for old-style polylists and new
style SpatialPolygons use external breaks and colours, because user needs
differ and advances in R itself (like colorRampPalette()) mean that
hardwiring things is not a robust option.
If maps need to be re-created often from the same arguments, a custom
function (placing the legend, choosing label sizes and placings, etc.) is
thw way to go using the ingredients above - a custom palette, custom
breaks - for instance using the same breaks on many different data sets,
and findInterval() for continuous-valued variables, and (with care)
as.integer() of factors for categorical variables.
Hope this helps - I think plot.Map will get a "deprecated" warning fairly
soon!
>
> I am running last R in XP updated.
>
> If anybody has a moment, I appreciate it a lot,
>
>
> Mihai Nica
>
> _________________________________________________________________
> Dont just search. Find. Check out the new MSN Search!
>
>
--
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