[R] Maps package, coloration
Michaell Taylor
michaell.taylor at boxwoodmeans.com
Fri Oct 14 00:49:23 CEST 2005
I am having some trouble getting the colors correct on county maps using
the maps package. I have a data.frame that contains coloration data for
every county -- it also contains a variable 'mapm' which fits the
'state,county' format used in the mapping package.
I use this to define colors from a range of 1:100. The problem is that
the colors seem good for some states/counties, but random for others.
It is almost as if the index has been scrambled, but I find that the
ordering is fine in the data.frame.
I am completely confused, any guidance as to what simple thing that I am
missing here. Any suggestions greatly appreciated.
data(countyMapEnv)
all.names <- map('county',namesonly=T)
all.names1 <- data.frame(mapm=all.names, col=NA,ordin=1:length
(all.names))
# created a dataframe, to be sure of the
# order, I include a variable 'ordin' to index later
# next I merge in the data to use for coloration
# the merge works well, with every element matched
t <- merge(final[,c('PVar','mapm')],all.names1)
# check order once more (redundant perhaps, but I was desperate)
t <- t[order(t$ordin),]
# a little accounting for the colors
t$PVar <- (t$PVar-min(t$PVar,na.rm=T))/(max(t$PVar,na.rm=T)-min(t
$PVar,na.rm=T))
t$col <- round((((t$PVar*100)-100)*-1),0)
# t$col is now a collection of integers from 1 to 100
# again, redundancy out of desperation.
matchm <- match.map('county',t$mapm,exact=T,warn=T)
# define colors with the heat.map palette
color <- heat.colors(100)[t$col[matchm]]
# alternative (also not working)
color <- heat.colors(100)[t$col]
# use the colors to fill the map
map('county',fill=T,col=color)
Michaell Taylor
--
================
Michaell Taylor, Phd
Principal
Boxwood Means, Inc
Two Stamford Landing
Suite 100
68 Southfield Avenue
Stamford, CT 06902
(T) 203-653-4100
More information about the R-help
mailing list