[R] Coloring counties on a full US map based on a certain criterion

Ray Brownrigg Ray.Brownrigg at ecs.vuw.ac.nz
Fri Jan 13 21:41:27 CET 2012


On 14/01/2012 8:04 a.m., Sarah Goslee wrote:
> On Fri, Jan 13, 2012 at 1:52 PM, Dimitri Liakhovitski
> <dimitri.liakhovitski at gmail.com>  wrote:
>> Just to clarify, according to help about the "fill" argument:
>> logical flag that says whether to draw lines or fill areas. If FALSE,
>> the lines bounding each region will be drawn (but only once, for
>> interior lines). If TRUE, each region will be filled using colors from
>> the col = argument, and bounding lines will not be drawn.
>> We have fill=TRUE - so why are the county borders still drawn?
>> Thank you!
>> Dimitri
>>
> This prompted me to check the code:
>
> if fill=TRUE, map() calls polygon()
> if fill=FALSE, map() calls lines()
>
> But polygon() draws borders by default.
>> plot(c(0,1), c(0,1), type="n")
>> polygon(c(0,0,1,1), c(0,1,1,0), col="yellow")
> To not draw borders, the border argument is provided:
>> plot(c(0,1), c(0,1), type="n")
>> polygon(c(0,0,1,1), c(0,1,1,0), col="yellow", border=NA)
> But that fails in map():
>> map('county', 'iowa', fill=TRUE, col=rainbow(20), border=NA)
> Error in par(pin = p) :
>    invalid value specified for graphical parameter "pin"
>
> because border is used as a named argument in map() already, for setting the
> size of the plot area, so there's no way to alter the border argument
> to polygon.
>
Coincidentally, I became aware of this just recently.  When the maps 
package was created (way back in the "'new' S" era), polygon() didn't 
add borders, and that is why ?map states that fill does not add 
borders.  A workaround is to change the map() option border= to 
myborder= (it is then used twice in map()).
> The work-around I suggested previous (lty=0) seems to be the only
> way to deal with the problem.
>
In fact I believe there is another workaround if you don't want to 
modify the code; use the option resolution=0 in the map() call. I.e. try 
(in Sarah's original Iowa example):

map('county', 'iowa', fill= TRUE, col = classcolors[countycol], 
resolution=0, lty=0)

This ensures that the polygon boundaries match up.

I'll fix the border issue in the next version of maps (*not* the one 
just uploaded to CRAN, which was to add Cibola County to NM).

Ray Brownrigg
> Sarah



More information about the R-help mailing list