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)
It is copied from page 6 in
Richard A. Becker, and Allan R. Wilks, "Maps in S", AT&T Bell
Laboratories Statistics Research Report [93.2], 1993.
http://public.research.att.com/areas/stat/doc/93.2.ps
I also wonder whether the county names are available in the database
used by map(), and, if yes, how to extract or utilize them.
Thanks!
Tord
--
Tord Sn?ll
Department of Conservation Biology
Swedish University of Agricultural Sciences (SLU)
P.O. 7002, SE-750 07 Uppsala, Sweden
Office/Mobile/Fax
+46-18-672612/+46-730-891356/+46-18-673537
E-mail: tord.snall at nvb.slu.se
www.nvb.slu.se/staff_tordsnall
since dr. bivand seems to be away, i will try to help a little :-)
i did not work with the database that comes with maps, but with shapefiles from
http://www.census.gov/geo/www/cob/co2000.html#shp. you need to merge the
dataframe that you have with the shapefile.
do you have access to gis software? the merging can be done with r but it is
quite cumbersome.
The road to plot the map could be:
_________________________________________________________________
require(maptools)
a=read.shape("your_merged_file.shp", dbf.data=TRUE, verbose=TRUE)
attach(a)
win.graph(width=5,height=3, pointsize=8)
plot.Map(a, auxvar=the_variable_to_plot_from_dbf_file, xlab=" ",
ylab=" ",
ol="NA")
__________________________________________________________________
more about this at: http://cran.r-project.org/src/contrib/Views/Spatial.html
hth,
Mihai Nica
170 East Griffith St. G5
Jackson, MS 39201
601-914-0361
----- Original Message ----
From: Tord Snäll <Tord.Snall@nvb.slu.se>
To: R-help@stat.math.ethz.ch
Sent: Wednesday, December 27, 2006 5:12:04 AM
Subject: [R] counties in different colours using map()
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)
It is copied from page 6 in
Richard A. Becker, and Allan R. Wilks, "Maps in S", AT&T Bell
Laboratories Statistics Research Report [93.2], 1993.
http://public.research.att.com/areas/stat/doc/93.2.ps
I also wonder whether the county names are available in the database
used by map(), and, if yes, how to extract or utilize them.
Thanks!
Tord
--
Tord Snäll
Department of Conservation Biology
Swedish University of Agricultural Sciences (SLU)
P.O. 7002, SE-750 07 Uppsala, Sweden
Office/Mobile/Fax
+46-18-672612/+46-730-891356/+46-18-673537
E-mail: tord.snall@nvb.slu.se
www.nvb.slu.se/staff_tordsnall
______________________________________________
R-help@stat.math.ethz.ch 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.
__________________________________________________
[[alternative HTML version deleted]]
The following example shows how to get/display the county names:
library(maps)
# Get County Data
m <- map('county', 'colorado', plot=FALSE)
names(m)
m$names # State,County names
# The names appear to be in alphabetical order by state,
e.g.:> m$names[1:3]
[1] "colorado,adams" "colorado,alamosa"
"colorado,arapahoe"
# Show county names on map
map.text('county', 'colorado', proj='bonne', param=45)
# Show county indices on map
map.text('county', 'colorado', proj='bonne', param=45,
labels=paste(1:length(m$names)))
#or perhaps
map.text('county', 'colorado', proj='bonne', param=45,
labels=paste(1:length(m$names)), col=1:length(m$names))
You can use your own labels vector above to show county abbreviations
instead of full names, or other info, if desired.
Once you get the mapping of the counties, you can connect to other sources
of information.
efg
"Tord Sn?ll" <Tord.Snall at nvb.slu.se> wrote in message
news:45925504.2060401 at nvb.slu.se...
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)
It is copied from page 6 in
Richard A. Becker, and Allan R. Wilks, "Maps in S", AT&T Bell
Laboratories Statistics Research Report [93.2], 1993.
http://public.research.att.com/areas/stat/doc/93.2.ps
I also wonder whether the county names are available in the database
used by map(), and, if yes, how to extract or utilize them.
Thanks!
Tord
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