Plotting color map with zip codes in R or Python

别等时光非礼了梦想. 提交于 2019-11-29 19:33:59
Eduardo Leoni

I am assuming you want static maps.


(source: eduardoleoni.com)

1) Get the shapefiles of the zip boundaries and state boundaries at census.gov:

2) Use the plot.heat function I posted in this SO question.

For example (assumes you have the maryland shapefiles in the map subdirectory):

library(maptools)
##substitute your shapefiles here
state.map <- readShapeSpatial("maps/st24_d00.shp")
zip.map <- readShapeSpatial("maps/zt24_d00.shp")
## this is the variable we will be plotting
zip.map@data$noise <- rnorm(nrow(zip.map@data))
## put the lab point x y locations of the zip codes in the data frame for easy retrieval
labelpos <- data.frame(do.call(rbind, lapply(zip.map@polygons, function(x) x@labpt)))
names(labelpos) <- c("x","y")                        
zip.map@data <- data.frame(zip.map@data, labelpos)
## plot it
png(file="map.png")
## plot colors
plot.heat(zip.map,state.map,z="noise",breaks=c(-Inf,-2,-1,0,1,2,Inf))
## plot text
with(zip.map@data[sample(1:nrow(zip.map@data), 10),] , text(x,y,NAME))
dev.off()
Shane

There are many ways to do this in R (see the spatial view); many of these depend on the "maps" package.

Someone may have something more direct for you, but I found O'Reilly's 'Data Mashups in R' very interesting... in part, it's a spatial mapping of home foreclosure auctions.

http://oreilly.com/catalog/9780596804770/

In Python, you can use shapefiles from the US census along with the basemap package. Here is an example of filling in states according to population.

There is a rich and sophisticated series of packages in R to plot, do analysis, and other functions related to GIS. One place to get started is the CRAN task view on Spatial Data: This is a complex and sometimes arcane world, and takes some work to understand.

If you are looking for a free, very functional mapping application, may I suggest:

MapWindow ( mapwindow.com)

Daniel Levine at TechCrunch Trends has done nice things with the maps package in R. He has code available on his site, too.

Paul's suggestion of looking into Processing - which Ben Fry used to make zipdecode - is also a good one, if you're up for learning a (Java-like) new language.

Depending on your application, a long way around might be to use something like this:

http://googlemapsmania.blogspot.com/2006/07/new-google-maps-us-zip-code-mashups.html

To map your data. If that wasn't quite what you wanted, you can get raw zip code shapefiles from census.gov and do it manually, which is quite a pain.

Also, if you haven't seen it, this is a neat way to interact with similar data, and might offer some pointers:

http://benfry.com/zipdecode/

Check out this excellent online visualization tool by IBM http://manyeyes.alphaworks.ibm.com/manyeyes/

EDIT FYI, ManyEyes uses the Prefuse visualization toolkit for some of its viz. Even though it is a java-based framework, they also provide a Flash/ActionScript tool for the web.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!