[R] counties in different colours using map()

Jim Lemon jim at bitwrit.com.au
Thu Dec 28 11:34:46 CET 2006


Tord Snäll wrote:
> Hi,
> I would like to plot a map of US counties using different colors. map() 
> seems to be the function to use, e.g.
> library(maps); map('usa'); map('county', 'colorado', add=T,fill = T, 
> col=c(1:5))
> plots Colorado counties using colours 1 to 5.
> 
> However, I want each color to represent a certain value - a value to be 
> picked from a data frame.
> This code should show a correspoding map at the level of states:
> state.names <- system('tr "[A-Z]" "[a-z]"', state.name)
> map.states <- unix('sed "s/:.*//"', map(names=T,plot=F))
> state.to.map <- match(map.states, state.names)
> color<- votes.repub[state.to.map, votes.year = 1900] / 100
> map('state', fill=T, col=color); map('state', add=T)
>
Hi Tord,
I don't know if this matches the color to the state as I couldn't get 
your "unix" function to work, but it does what I think you want.

library(maps)
map("usa")
data(votes.repub)
library(plotrix)
state.col<-color.scale(votes.repub[,30],c(0,1),0,c(1,0))
map("state",fill=TRUE,col=state.col)

Jim



More information about the R-help mailing list