[R] Plotting data on a US County Map

mathijsdevaan mathijsdevaan at gmail.com
Sun Apr 3 12:15:50 CEST 2011


Hi, 

I have a data frame listing US counties and a quantity ("number") per county
and I have a shapefile of the US with county ID's. I would like to plot the
"number" variable on a map (in the shapefile) using a color range per county
(e.g. white = min(number) = 2, black = max(number) = 15). Can anyone help me
actually plotting the data on the map? This is how far I got. Thanks!   

DF = data.frame(read.table(textConnection(" A CNTY_FIPS number 
1 US001 2 
2 US002 8 
3 US003 3 
4 US004 5 
5 US005 6 
6 US006 7 
7 US007 9 
8 US008 9 
9 US009 10 
10 US010 11 
11 US011 13 
12 US012 15"),head=TRUE,stringsAsFactors=FALSE)) 

library(maptools) 
library(ggplot2) 
library(gpclib) 
gpclibPermit() 
setwd("C:/Documents") 
us_counties.shp <- readShapeSpatial("uscounties.shp") 
us_counties.shp.p <- fortify.SpatialPolygonsDataFrame(us_counties.shp,
region="CNTY_FIPS") 
us <- merge(us_counties.shp.p, us_counties.shp, by.x="id", by.y="CNTY_FIPS") 
p <- ggplot(data=us, aes(x=long, y=lat, group=group)) + 
geom_polygon(fill="#CFCFCF") 
p <- p + geom_path(color="white") + coord_equal() 
ggsave(p, width=11.69, height=8.27, file="us_counties_a.jpg")

--
View this message in context: http://r.789695.n4.nabble.com/Plotting-data-on-a-US-County-Map-tp3423342p3423342.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list