用R开发地理专题地图

R中有很多包可以进行各种空间分析。 这可以通过在CRAN任务视图:空间数据分析中看到。 这些软件包是多种多样的,但我想要做的只是一些简单的专题地图 。 我有县和州的FIPS代码的数据,我有县和州边界的ESRI形状文件和随附的FIPS代码,允许join数据。 如果需要,形状文件可以很容易地转换为其他格式。

那么用R创build专题地图最直接的方法是什么?

这张地图看起来像是用ESRI Arc产品创build的,但是这是我想用R做的事情:

替代文字http://www.infousagov.comhttp://img.dovov.comchoro.jpg 从这里复制的地图。

下面的代码给了我很好的帮助。 自定义一下,你就完成了。 替代文字http://files.eduardoleoni.com/map.png

library(maptools) substitute your shapefiles here state.map <- readShapeSpatial("BRASIL.shp") counties.map <- readShapeSpatial("55mu2500gsd.shp") ## this is the variable we will be plotting counties.map@data$noise <- rnorm(nrow(counties.map@data)) 

热图function

 plot.heat <- function(counties.map,state.map,z,title=NULL,breaks=NULL,reverse=FALSE,cex.legend=1,bw=.2,col.vec=NULL,plot.legend=TRUE) { ##Break down the value variable if (is.null(breaks)) { breaks= seq( floor(min(counties.map@data[,z],na.rm=TRUE)*10)/10 , ceiling(max(counties.map@data[,z],na.rm=TRUE)*10)/10 ,.1) } counties.map@data$zCat <- cut(counties.map@data[,z],breaks,include.lowest=TRUE) cutpoints <- levels(counties.map@data$zCat) if (is.null(col.vec)) col.vec <- heat.colors(length(levels(counties.map@data$zCat))) if (reverse) { cutpointsColors <- rev(col.vec) } else { cutpointsColors <- col.vec } levels(counties.map@data$zCat) <- cutpointsColors plot(counties.map,border=gray(.8), lwd=bw,axes = FALSE, las = 1,col=as.character(counties.map@data$zCat)) if (!is.null(state.map)) { plot(state.map,add=TRUE,lwd=1) } ##with(counties.map.c,text(x,y,name,cex=0.75)) if (plot.legend) legend("bottomleft", cutpoints, fill = cutpointsColors,bty="n",title=title,cex=cex.legend) ##title("Cartogram") } 

绘制它

 plot.heat(counties.map,state.map,z="noise",breaks=c(-Inf,-2,-1,0,1,2,Inf)) 

以为我会在这里添加一些新的信息,因为自发布以来围绕这个主题已经有一些活动。 以下是革命博客上的“Choropleth Map R挑战”的两个很好的链接:

Choropleth地图R挑战

Choropleth挑战结果

希望这些对于查看这个问题的人有用。

祝一切顺利,

松鸦

检查包裹

 library(sp) library(rgdal) 

这对地理数据很好,而且

 library(RColorBrewer) 

对着色很有用。 这张地图是用上面的包和这个代码制作的:

 VegMap <- readOGR(".", "VegMapFile") Veg9<-brewer.pal(9,'Set2') spplot(VegMap, "Veg", col.regions=Veg9, +at=c(0.5,1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5), +main='Vegetation map') 

"VegMapFile"是一个shape文件, "Veg"是显示的variables。 做一点工作大概可以做得更好。 我似乎没有被允许上传图片,这里是一个图像的链接:

看看PBSmapping软件包(参见borh的小插曲/手册和演示)和这篇 O'Reilly Data Mashups in R的文章(不幸的是它不是免费的,但它的价值4.99美元下载,根据革命博客 )。

这只是三条线!

 library(maps); colors = floor(runif(63)*657); map("state", col = colors, fill = T, resolution = 0) 

完成! 只要将第二行更改为63个元素(0到657之间的每个元素,它们是颜色()的成员)

现在,如果你想要看到你可以写:

 library(maps); library(mapproj); colors = floor(runif(63)*657); map("state", col = colors, fill = T, projection = "polyconic", resolution = 0); 

63个元素代表了你可以通过运行得到的63个区域:

 map("state")$names; 

R Graphics Gallery有一个非常相似的地图 ,它应该是一个很好的起点。 代码在这里:www.ai.rug.nl/~hedderik/R/US2004。 您需要添加legend()函数的图例。