Sorry, I was talking nonsense.
The actual problem was in your panel function, where you extract the
state boundaries, and then draw them using panel.polygon: the order of
states from map() is arbitrary, and does not correspond to the color
palette that you set up.
I suggest using mapplot(). This matches the region names you provide
to the names from the map. However (as in your original case) you do
need to ensure that they match exactly, including case and sub-region
names...
i.e. you need to match up
NatSTSummaryHigh.abi$JurisdtnStateName with
map("state", regions=NatSTSummaryHigh.abi$JurisdtnStateName)$names
(exactly).
nclr <- 4
plotclr <- brewer.pal(nclr,"PuRd")
class <- classIntervals(NatSTSummaryHigh.abi$STMean, nclr,
style="fisher" )
mapplot(tolower(JurisdtnStateName) ~ STMean, data = NatSTSummaryHigh.abi,
map = us.map, breaks = class$brks, colramp = colorRampPalette(plotclr))
-Felix
On 18 May 2010 02:37, Anderson, Chris <chris.anderson at paradigmcorp.com>
wrote:> Flex,
>
> I apologize for not sending the data. I have attached the Rdata set and an
excel version and I've attached the graph. I have not use mapplot before is
that a better option if so then will you pass along the syntax I would use. As
far as using the current logic, I tried adding your suggesting to the groups
variable and I get the same result. I also have a field within my data called
BdgtGrp, which I placed in the groups option, but the state colors don?t match
the levels I have in the legend. Idealy, I would like those states that I have
identified with BdgtGrp=="Significanlty high" be in "red",
purple for those identified as "Mid-High" and light blue for those
identified as "High". If not by these flags then I would want the
colors be for the ranges specified within the class level.
>
> In any case, I expect Ohio, followed by New Mexico, and Arizona to be in
"red". In order for me to get the states highlighted in the proper
color are you suggesting I do a reorder on ?this section of the code
"core.states=map("state",
regions=NatSTSummaryHigh.abi$JurisdtnStateName[subscripts],
> ?plot=FALSE, fill=TRUE)" ?
>
>
>
>
> Chris Anderson
> Data Analyst
> Medical Affairs
> wk: 925-677-4870
> cell: 707-315-8486
> Fax:925-677-4670
>
>
> -----Original Message-----
> From: foolish.android at gmail.com [mailto:foolish.android at gmail.com] On
Behalf Of Felix Andrews
> Sent: Saturday, May 15, 2010 6:41 AM
> To: Anderson, Chris
> Cc: r-help at R-project.org
> Subject: Re: [R] help color coding map in R
>
> The 'groups' argument should be a factor, which explicitly defines
the
> ordering of its levels. Otherwise it is converted to a factor using
> the default ordering which is alphabetical. You can make a factor
> ordered by occurence as, eg., factor(z, levels = unique(z)). Or use
> reorder().
>
> Note that data attachments don't work on this list. After constructing
> a minimal example, it is best to dump the required objects in a
> pastable form using dput().
>
> I'm guessing you have some reason that you are not using mapplot()...
>
> -Felix
>
> On Saturday, May 15, 2010, Anderson, Chris
> <chris.anderson at paradigmcorp.com> wrote:
>> I am trying to create a map with selected states based on highest to
lowest mean cost. The following code properly selects the correct states, and
the legend is properly color coded with ranges, but the colors per range does
not match the state colors. I need help getting the state colors to match the
ranges outlined in the legend. I have tried ordering the mean amounts and this
correctly creates the vector of colors in the correct order, but when applied to
the map the colors don't match. Attached is the R dataset of my data. Please
help me tweak the map so the colors are properly assigned.
>>
>> # Get the entire US map for use later.
>> us.map <- map("state", plot = FALSE, fill = TRUE)
>>
>> # Calculate the range of the map (with extra margins).
>> xl <- extendrange(us.map$range[1:2])
>> yl <- extendrange(us.map$range[3:4])
>>
>> library(maps)
>> library(lattice)
>> library(latticeExtra)
>> library(RColorBrewer) # creates nice color schemes
>> library(classInt)
>>
>>
>> plotclr <- brewer.pal(nclr,"PuRd")
>> class <- classIntervals(NatSTSummaryHigh.abi$STMean, nclr,
style="fisher" )
>> colcode <- findColours(class, plotclr)
>>
>>
>> # Plot a multi-panel map of all the states, and colour
>> xyplot(y~x | NatSTSummaryHigh.abi$PrimaryDX, data =
state.center,groups=names(attr(colcode, "table")),
>> ?? ?main="High Cost States by Diagnosis ( > National
Avg)",
>> ?? ?xlim = xl, ylim = yl, scales = list(draw=FALSE),
>> ?? ?aspect = "iso",
>> ?? ?xlab = NULL, ylab = NULL,
>> ?? ?strip = strip.custom(var.name="Diagnosis", sep=":
",
>> ?? ?strip.names=TRUE, strip.levels=TRUE),
>> ??
?auto.key=list(border=TRUE,space="right",text=names(attr(colcode,
"table"))),
>> ?? ?par.settings = simpleTheme(pch=16,col=attr(colcode,
"palette"),cex=0.9),
>> ?? ? subscripts = TRUE,
>> ?? ?panel = function(..., subscripts) {
>> ?? ? ? ?# Plot the entire US map.
>> ?? ? ? ?panel.polygon(us.map$x,us.map$y)
>> ?? ? ? ?# Select the states to plot.
>> ?? ? ? ?core.states=map("state",
regions=NatSTSummaryHigh.abi$JurisdtnStateName[subscripts],
>> ?? ? ? ? ? ?plot=FALSE, fill=TRUE)
>>
>> ?? ? ? ?# Plot the states.
>> ?? ? ? ?panel.polygon(core.states$x, core.states$y,
>> ?? ? ? ? ? ?col = trellis.par.get("superpose.line")$col)
>> ?? ?} )
>>
>>
>> Chris Anderson
>> Data Analyst
>> Medical Affairs
>> wk: 925-677-4870
>> cell: 707-315-8486
>> Fax:925-677-4670
>>
>>
>> </pre><br>This electronic message transmission, including
any attachments, contains <br>information which may be confidential,
privileged and/or otherwise exempt <br>from disclosure under applicable
law. The information is intended to be for the <br>use of the
individual(s) or entity named above. If you are not the intended
<br>recipient or the employee or agent responsible for delivering the
message <br>to the intended recipient, you are hereby notified that any
disclosure, copying, <br>distribution or use of the contents of this
information is strictly prohibited. ?If <br>you have received this
electronic transmission in error, please notify the sender <br>immediately
by telephone (800-676-6777) or by a "reply to sender only"
<br>message and destroy all electronic and hard copies of the
communication, <br>including attachments. ?Thank
you.<br><br>For more information on Paradigm Management Services,
LLC, please visit <br>http://www.paradigmcorp.com <br>
>>
>
> --
> Felix Andrews / ???
> Postdoctoral Fellow
> Integrated Catchment Assessment and Management (iCAM) Centre
> Fenner School of Environment and Society [Bldg 48a]
> The Australian National University
> Canberra ACT 0200 Australia
> M: +61 410 400 963
> T: + 61 2 6125 4670
> E: felix.andrews at anu.edu.au
> CRICOS Provider No. 00120C
> --
> http://www.neurofractal.org/felix/
>
>
>
> This electronic message transmission, including any attachments, contains
> information which may be confidential, privileged and/or otherwise exempt
> from disclosure under applicable law. The information is intended to be for
the
> use of the individual(s) or entity named above. If you are not the intended
> recipient or the employee or agent responsible for delivering the message
> to the intended recipient, you are hereby notified that any disclosure,
copying,
> distribution or use of the contents of this information is strictly
prohibited. ?If
> you have received this electronic transmission in error, please notify the
sender
> immediately by telephone (800-676-6777) or by a "reply to sender
only"
> message and destroy all electronic and hard copies of the communication,
> including attachments. ?Thank you.
>
> For more information on Paradigm Management Services, LLC, please visit
> http://www.paradigmcorp.com
>
--
Felix Andrews / ???
Postdoctoral Fellow
Integrated Catchment Assessment and Management (iCAM) Centre
Fenner School of Environment and Society [Bldg 48a]
The Australian National University
Canberra ACT 0200 Australia
M: +61 410 400 963
T: + 61 2 6125 4670
E: felix.andrews at anu.edu.au
CRICOS Provider No. 00120C
--
http://www.neurofractal.org/felix/