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

Sarah Goslee sarah.goslee at gmail.com
Fri Jan 13 18:21:55 CET 2012


On Fri, Jan 13, 2012 at 12:15 PM, Dimitri Liakhovitski
<dimitri.liakhovitski at gmail.com> wrote:
> Sarah, this is amazing, thank you so much.
> One question: I am trying to do for the whole US (on one map) what
> you've helped me do for Iowa.
> In other words, I would like to create a file of inputs like you
> "countycol " with 1,000+ lines - for all US counties (probably without
> Hawaii and Alaska, right?) and then somehow feed it into the map
> command but so that the output is the whole map of the US, and not one
> state. Is it at all possible?

Sure. Just start with
map('county')
instead.
I like to add something like:
map('state', lwd=3, add=TRUE)

You'll need to instead coordinate with the names of the entire US:
> length(map('county', plot=FALSE)$names)
[1] 3082

Sarah

> Or maybe it's possible to create 48 colored state maps one by one -
> the way you showed me - save them, and then somehow "paste" those
> states onto the whole US map?
> Thanks a lot for your help!
> Dimitri
>
> On Fri, Jan 13, 2012 at 12:05 PM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
>> Hi,
>>
>> You've just about got it. See below.
>>
>> On Fri, Jan 13, 2012 at 11:52 AM, Dimitri Liakhovitski
>> <dimitri.liakhovitski at gmail.com> wrote:
>>> Dear Rers,
>>>
>>> is there a way to color counties on a full US map based on a criterion
>>> one establishes (i.e., all counties I assign the same number should be
>>> the same color)?
>>> I explored a bit and looks like the package "maps" might be of help.
>>> library(maps)
>>> One could get a map of the US: map('usa')
>>> One could get countries within a US state: map('county', 'iowa', fill
>>> = TRUE, col = palette())
>>
>> Using a random sampling to give you the basic idea.
>> There are 99 counties in Iowa, so to construct the criterion:
>> countycol <- sample(1:5, 99, replace=TRUE)
>> And to invent a set of colors (RColorBrewer is a better choice for
>> final maps):
>> classcolors <- rainbow(5)
>>
>> then you can use them in your map just as you would for any other
>> plotting command:
>>
>> map('county', 'iowa', fill= TRUE, col = classcolors[countycol])
>>
>>> Would it be possible to read in a file with counties and their
>>> assignments (some counties have a 1, some counties have a 2, etc.) and
>>> then have one map of the US with counties colored based on their
>>> assignment?
>>
>> Absolutely. The only thing you have to watch out for is that you put your
>> values in the same order as:
>> map('county', 'iowa', plot=FALSE)$names
>>


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list