[R] color.ramp in maptools

Roger Bivand Roger.Bivand at nhh.no
Fri Mar 26 18:56:54 CET 2004


On Fri, 26 Mar 2004, Marco Albani wrote:

> Dear list members,
> 
> I am trying to use the maptools library to display geographical data. At 
> the moment I have some trouble understanding how the " auxvar " variable 
> is supposed to be used in the plot.Map function.
> 
> I am using R Version 1.8.1  (2003-11-21) on Linux
> 
> Looking at the plot.Map function itself, I see that it calls a 
> color.ramp function (I am reporting only the relevant part here):
> 
> ...
>      if (attr(theMap$Shapes, "shp.type") == "poly") {
>          if (!is.null(auxvar) && nclass > 1) {
>              col.rmp <- color.ramp(nclass, nvec = auxvar)
>              for (i in recs) {
> ...
> 
> 
> I don't seem to be able to get any information on this color.ramp 
> function. In fact the function doesn't seem to exist if I search for it 
> with ls()
> 
> Does anyone have any insight?

The function is not exported in the package namespace (you can read it
using the ::: operator: maptools:::color.ramp will display it). Its usage
is: color.ramp(nclass,color='red',nvec=NULL,type='q'), where: nclass is
the number of classes desired in the ramp; color is the base color to
build the ramp on; nvec is the numeric vector (or factor) from which to 
build the ramp; and type is the type of binning procedure to use (default 
quantiles, if set to "e", it will use equal-sized bins.). 

In general, most users quantize and colour using the fg= argument to
plot.Map() directly, so the color.ramp() arguments to plot.Map() are
seldom used, which is also why the function is not documented or exported
to user space. color.ramp() uses cut() internally to assign the variable 
to intervals, while the prefered function in user space is findInterval(), 
used for tabular lookup in a colour table. I hope this provides the 
information you might need - my general advice would be to create the 
interval breaks before calling plot.Map(), setting:

fg=cols[findInterval(x=nvec, vec=breaks, all.inside=TRUE)]

for numeric variables and a chosen colour palette (see RColorBrewer), and 
using the levels of factors as appropriate for factors (more or less as 
in the example for plot.Map()).

Thanks for choosing a very specific and accurate subject!

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Breiviksveien 40, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 93 93
e-mail: Roger.Bivand at nhh.no




More information about the R-help mailing list