用R或Python绘制带有邮政编码的彩色地图

2024-05-13 07:01:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些美国人口统计和坚实的数据。
我想绘制一个州或一个较小区域(如城市)的zipcode区域。每个区域都将由特定于该区域的颜色和/或文本进行注释。输出类似于http://maps.huge.info/,但a)带有注释文本;b)pdf输出;c)可在R或Python中编写脚本。

是否有任何包和代码允许我这样做?


Tags: 数据代码文本info脚本http区域pdf
3条回答

我想你需要静态地图。

alt text
(来源:eduardoleoni.com

1)在census.gov上获取zip边界和state边界的形状文件:

2)使用我在SO question中发布的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()

在R中有很多方法可以做到这一点(参见spatial view);其中很多方法都是depend on the "maps" package

有人可能对你有更直接的建议,但我发现O'Reilly在R中的数据混搭非常有趣。。。在某种程度上,这是房屋止赎拍卖的空间映射。

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

相关问题 更多 >