[R] help color coding map in R

Felix Andrews felix at nfrac.org
Sat May 15 15:40:47 CEST 2010

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

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()...


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

More information about the R-help mailing list