用R开发地理专题地图 [英] Developing Geographic Thematic Maps with R

查看:115
本文介绍了用R开发地理专题地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于各种空间分析,R中有很多包。这可以在 CRAN任务视图:空间数据分析中看到。这些软件包众多,多样化,但我想做的只是一些简单的地图。我有县和州的FIPS代码的数据,我有县和州边界的ESRI形状文件和附带的FIPS代码,允许加入数据。如果需要,形状文件可以轻松地转换成其他格式。



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



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



alt text http://www.infousagov.com/images/choro .jpg 地图从这里复制

解决方案

以下代码为我服务。自定义一点,你完成了。
替代文字http://files.eduardoleoni.com/map.png

 库(maptools)
在这里替换你的shapefile
state.map< - readShapeSpatial(BRASIL.shp)
counties.map< - readShapeSpatial(55mu2500gsd.shp)
##这是我们将要绘制的变量
counties.map@data$noise< - rnorm(nrow(counties.map @data))

heatmap function

  plot.heat<  -  function(counties.map,state.map,z,title = NULL,breaks = NULL,reverse = FALSE,cex.legend = 1,bw = col.vec = NULL,plot.legend = TRUE){
##分解值变量
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 ],断裂,include.lowest = TRUE )
cutpoints< - levels(counties.map@data$zCat)
if(is.null(col.vec))col.vec< - heat.colors(length(levels map @ data $ zCat)))
if(reverse){
cutpointsColors< - rev(col.vec)
} else {
cutpointsColors< - col.vec
}
级别(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
##标题(Cartogram)
}

绘图

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


There are clearly a number of packages in R for all sorts of spatial analysis. That can by seen in the CRAN Task View: Analysis of Spatial Data. These packages are numerous and diverse, but all I want to do is some simple thematic maps. I have data with county and state FIPS codes and I have ESRI shape files of county and state boundaries and the accompanying FIPS codes which allows joining with the data. The shape files could be easily converted to other formats, if needed.

So what's the most straight forward way to create thematic maps with R?

This map looks like it was created with an ESRI Arc product, but this is the type of thing I would like to do with R:

alt text http://www.infousagov.com/images/choro.jpg Map copied from here.

解决方案

The following code has served me well. Customize it a little and you are done. alt text 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))

heatmap 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 it

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

这篇关于用R开发地理专题地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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