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

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Fri Jan 13 22:32:18 CET 2012


Everyone, thanks a lot - this is great:

### My criterion for all counties:
allcounties<-data.frame(county=map('county', plot=FALSE)$names)
allcounties$group<-c(rep(1:6,513),rep(1,4))[order(c(rep(1:6,513),rep(1,4)))]
### My colors:
classcolors <- rainbow(6)

### 1. If I want to have no borders between counties:
map('county',fill=TRUE,col=classcolors[allcounties$group],resolution=0,lty=0,bg
= "transparent")
map('state', lwd=1, add=TRUE)

##### 2. If I want to see borders between counties (of a desired
color, e.g., gray):
### For line color:
oldpar <- par(fg='gray') # change the default fg (foreground color) to white
### My US map:
map('county',fill=TRUE,col=classcolors[allcounties$group],lty=1,bg =
"transparent")
par(oldpar)

Dimitri

On Fri, Jan 13, 2012 at 3:48 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
> Hi Ray,
>
> I'm glad to see you here. I was going to write this up a bit more
> clearly and email it to you, but now I don't have to bother. :)
>
>> 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()).
>
> I though it was probably a legacy code issue.
>
>> 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.
>
> Ah! That works nicely, and wasn't clear to me from the help that it
> would do so.
>
> Thanks!
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Dimitri Liakhovitski
marketfusionanalytics.com



More information about the R-help mailing list