gap*_*ppy 30 python graphics zipcode r
我有一些美国人口统计和公司数据.
我想在一个州或一个较小的地区(例如城市)绘制邮政编码区域.每个区域将由特定于该区域的颜色和/或文本注释.输出类似于http://maps.huge.info/,但a)带有注释文本; b)pdf输出; c)R或Python中的脚本.
是否有任何包和代码可以让我这样做?
Edu*_*oni 38
我假设你想要静态地图.
alt text http://files.eduardoleoni.com/mapUS.png
1)在census.gov 获取zip边界和状态边界的shapefile:
2)使用我在这个SO问题中发布的plot.heat函数.
例如(假设您在map子目录中有maryland shapefile):
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()
Run Code Online (Sandbox Code Playgroud)
Sha*_*ane 10
在R中有很多方法可以做到这一点(参见空间视图); 其中许多依赖于"地图"包.
看看这个美国2004年大选的一个很酷的例子.最终看起来像这样:alt text http://www.ai.rug.nl/~hedderik/R/US2004/US04Election-PopGraded.png
这是一个使用"maps"包和"lattice"的模型的一个稍微丑陋的例子.
这是一个使用"gmaps"软件包的非常简单的示例,其中显示了针对谋杀案每10万人的逮捕状态的逮捕地图:
require(gmaps)
data(USArrests)
attach(USArrests)
grid.newpage()
grid.frame(name="map")
grid.pack("map",USALevelPlot(states=rownames(USArrests),levels=Murder,col.fun=reds),height=unit(1,'null'))
grid.pack("map",gradientLegendGrob(at=quantile(Murder),col.fun=reds),side="bottom",height=unit(.2,'npc'))
detach(USArrests)
Run Code Online (Sandbox Code Playgroud)