gappy gappy - 1 year ago 60
R Question

Plotting color map with zip codes in R or Python

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 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?

Answer Source

I am assuming you want static maps.

alt text

1) Get the shapefiles of the zip boundaries and state boundaries at

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

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

##substitute your shapefiles here <- readShapeSpatial("maps/st24_d00.shp") <- readShapeSpatial("maps/zt24_d00.shp")
## this is the variable we will be plotting$noise <- rnorm(nrow(
## put the lab point x y locations of the zip codes in the data frame for easy retrieval
labelpos <- data.frame(, lapply(, function(x) x@labpt)))
names(labelpos) <- c("x","y")                <- data.frame(, labelpos)
## plot it
## plot colors
## plot text
with([sample(1:nrow(, 10),] , text(x,y,NAME))