I'm trying to create a colored map that would show the number of students
per state.
My data frame consists of two columns - state and count.
I'm using the following code
library(maps)
map("usa")
library(plotrix)
state.col<-color.scale(gre$count,0,0,c(0,1))
map("state",fill=TRUE,col=state.col)
I'm getting a map, but the values are not being mapped to correct states.
What do I need to do to fix that?
[[alternative HTML version deleted]]
Here's a bit of a hacky way to do it:
#get the names of each state
state=map('state',plot=F)$names
#set up some random state-color data
cols =
as.data.frame(cbind(state=states,x=sample(1:10,length(states),replace=T)))
#do the plot
map('usa')
for(this_state in state){
map('state',region=this_state,add=T,fill=T,col=cols$x[cols$state==this_state])
}
On Thu, Feb 19, 2009 at 12:45 PM, Alina Sheyman <alinashe at gmail.com>
wrote:> I'm trying to create a colored map that would show the number of
students
> per state.
> My data frame consists of two columns - state and count.
> I'm using the following code
>
> library(maps)
> map("usa")
> library(plotrix)
> state.col<-color.scale(gre$count,0,0,c(0,1))
> map("state",fill=TRUE,col=state.col)
>
> I'm getting a map, but the values are not being mapped to correct
states.
> What do I need to do to fix that?
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Mike Lawrence
Graduate Student
Department of Psychology
Dalhousie University
www.thatmike.com
Looking to arrange a meeting? Check my public calendar:
http://www.thatmike.com/mikes-public-calendar
~ Certainty is folly... I think. ~
The problem is that the order and number of polygons in map does not match your
data (most likely).
Try:
library(maps)
library(plotrix)
state.col<-color.scale(state.x77[,'HS Grad'],0,0,c(0,1))
map("state",fill=TRUE,col=state.col)
nms <- map('state', plot=FALSE)$names
nms <- sub(':.*$','',nms)
ind <- match(nms, tolower(state.name))
map('state', fill=TRUE, col=state.col[ind])
Now try it with your data, just replace state.name with the names of the states
in the order of your data if it is different (possibly the rownames).
You may also want to look at state.vbm in the TeachingDemos package (works with
maptools rather than maps) as another way to plot colored maps of the USA that
is less affected by geographical area.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Alina Sheyman
> Sent: Thursday, February 19, 2009 9:46 AM
> To: r-help at r-project.org
> Subject: [R] colored maps again
>
> I'm trying to create a colored map that would show the number of
> students
> per state.
> My data frame consists of two columns - state and count.
> I'm using the following code
>
> library(maps)
> map("usa")
> library(plotrix)
> state.col<-color.scale(gre$count,0,0,c(0,1))
> map("state",fill=TRUE,col=state.col)
>
> I'm getting a map, but the values are not being mapped to correct
> states.
> What do I need to do to fix that?
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.