Plotting color map with zip codes in R or Python
Asked Answered
V

8

32

I have some US demographic and firmographic data.
I would like to plot zipcode areas in a state or a smaller region (e.g. city). Each area would be annotated by color and/or text specific to that area. The output would be similar to http://maps.huge.info/ but a) with annotated text; b) pdf output; c) scriptable in R or Python.

Is there any package and code that allows me to do this?

Vinaigrette answered 17/9, 2009 at 22:50 Comment(0)
B
39

I am assuming you want static maps.

alt text
(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()
Byproduct answered 18/9, 2009 at 18:21 Comment(3)
The links to to shape files at www.census.gov are broken... it took me a while to find them. Try this URL: census.gov/cgi-bin/geo/shapefiles2010/main. Then use the dropdown to select "Zip Code Tabulation Areas" and "States (and equivalent)."Telecommunication
Do you mind explaining how I can map values from a csv file that has a zip code column and some other data columns to use this (apologies if the answer is obvious, but I don't really know R at all)? Specifically, I am having trouble figuring out what I should put for zip.map@data$noise <- rnorm(nrow(zip.map@data)) labelpos <- data.frame(do.call(rbind, lapply(zip.map@polygons, function(x) x@labpt))) and zip.map@data <- data.frame(zip.map@data, labelpos)Bloodstain
If that site is down for maintenance, you can use this link: census.gov/cgi-bin/geo/shapefiles/index.phpAnselm
B
10

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

Barbusse answered 18/9, 2009 at 1:51 Comment(1)
It seems like these examples are at the county, not zipcode, levelMusty
D
3

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/

Deuced answered 17/9, 2009 at 23:21 Comment(1)
Plus, no DRM and only $5...hard to beat that deal!Bename
G
3

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.

Grimace answered 9/12, 2009 at 4:38 Comment(0)
F
1

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)

Folkestone answered 18/9, 2009 at 1:48 Comment(1)
I could not find anything in that CRAN view that would help me visualize zip code statistics on a map. The closest I got was the package muRL.Vinaigrette
Y
1

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.

Yarrow answered 18/9, 2009 at 1:50 Comment(1)
thanks Matt, yes the trends maps are zip code level, but instead of shading zipcode areas, I actually mapped the zipcodes to lat/long coords. Anyone is welcome to the code though.Classmate
S
0

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/

Shippy answered 17/9, 2009 at 23:11 Comment(1)
nice too, but these are visualizations of zip codes locations/boundaries. I am looking for a flexible way in R or Python to generate maps with custom-colored or text-annotated zip regions.Vinaigrette
H
0

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.

Hollington answered 17/9, 2009 at 23:19 Comment(2)
Isnt that the same thing that was presented by Hans Rosling on TED a few years agoHollington
nice, but manyeyes doesn't answer my question. I think it's very different from gapminder. Wattenberg is a visualization guy, Rosling is a social scientist, and the different approach shows.Vinaigrette

© 2022 - 2024 — McMap. All rights reserved.