用R或Python绘制邮政编码彩色地图 [英] Plotting color map with zip codes in R or Python

查看:137
本文介绍了用R或Python绘制邮政编码彩色地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些美国的人口统计数据和公司数据。

我想绘制一个州或更小区域(如城市)的邮政编码区域。每个区域都会用特定于该区域的颜色和/或文本进行注释。输出将类似于 http://maps.huge.info/ ,但a)带注释的文本; b)pdf输出; c)用R或Python编写脚本。



是否有包和代码允许我执行此操作?

解决方案

我假设你想要静态地图。



alt text http://files.eduardoleoni .com / mapUS.png



<1>获取 zip 边界和 state 边界:


2)使用我在这个问题

例如(假设你有maryland shapefile在map子目录中):

pre $ library
##在这里替换你的shape文件
state.map< - readShapeSpatial(maps / st24_d00.shp)
zip.map< - readShapeSpatial(maps / zt24_d00.shp)
##这是我们将要绘制的变量
zip.map@data$noise< - rnorm(nrow(zip.map@d ata))
##将实验点xy邮政编码位置放在数据框中以便于检索
labelpos< - data.frame(do.call(rbind,lapply(zip.map @多边形,函数(x)x @ labpt)))
名称(labelpos)< -c(x,y)
zip.map@data< - data.frame(zip .map @ data,labelpos)
##绘制它
png(file =map.png)
##绘制颜色
plot.heat(zip.map,state .map,z =noise,breaks = c(-Inf,-2,-1,0,1,2,Inf))
##绘制文本
(zip.map@data [sample(1:nrow(zip.map@data),10),],text(x,y,NAME))
dev.off()
pre>

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?

解决方案

I am assuming you want static maps.

alt text http://files.eduardoleoni.com/mapUS.png

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()

这篇关于用R或Python绘制邮政编码彩色地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆