[英]Developing Geographic Thematic Maps with R
R中有很多包用于各种空间分析。 这可以在CRAN任务视图中看到:空间数据分析 。 这些软件包数量众多且各种各样,但我想做的只是一些简单的专题图 。 我有县和州FIPS代码的数据,我有县和州边界的ESRI形状文件和随附的FIPS代码,允许加入数据。 如果需要,形状文件可以很容易地转换为其他格式。
那么用R创建专题地图最直接的方法是什么?
这张地图看起来像是用ESRI Arc产品创建的,但这是我想用R做的事情:
alt text http://www.infousagov.com/images/choro.jpg 从这里复制的地图。
以下代码对我很有帮助。 稍微定制它就完成了。
(来源: eduardoleoni.com )
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))
热图功能
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))
以为我会在这里添加一些新信息,因为自发布以来,围绕此主题已经有了一些活动。 以下是Revolutions博客上“Choropleth Map R Challenge”的两个很棒的链接:
希望这些对于查看此问题的人有用。
祝一切顺利,
松鸦
看看包裹
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"
是一个shapefile, "Veg"
是显示的变量。 一点点工作可能会做得更好。 我似乎不允许上传图片,这里是图片的链接:
看一下PBSmapping包(请参阅插图/手册和演示)以及这篇 O'Reilly Data Mashups的R文章(不幸的是它不是免费的,但根据Revolutions博客下载价值4.99美元)。
这只是三行!
library(maps);
colors = floor(runif(63)*657);
map("state", col = colors, fill = T, resolution = 0)
完成! 只需将第二行更改为63个元素的任何向量(0到657之间的每个元素,它们是colors()的成员)
现在,如果你想得到幻想,你可以写:
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()函数添加图例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.