GADM-Maps跨国比较图形

spe*_*ndo 10 graphics maps r

也许是因为我对R比较新,我在http://www.gadm.org/上使用gadm-Mapfiles时遇到了问题.

我尝试绘制一个包含多个国家/地区的地图并将它们相互比较(使用不同的颜色).

这就是我的工作

library('sp')
##
load(url('http://biogeo.ucdavis.edu/data/gadm2/R/ARG_adm0.RData')) 
# loads an Object "gadm" with shape of Argentinia
arg <- gadm # is there a more convenient way to do this in one line?
load(url('http://biogeo.ucdavis.edu/data/gadm2/R/CHL_adm0.RData'))
# loads an Object "gadm" with shape of Chile
chl <-gadm
load(url('http://biogeo.ucdavis.edu/data/gadm2/R/BOL_adm0.RData'))
# loads an Object "gadm" with shape of Bolivia
bol <- gadm
##
spplot(c(arg, chl, bol))
# output: unable to find an inherited method for function "spplot", for signature "list"
Run Code Online (Sandbox Code Playgroud)

这是我的问题:

  1. (这个问题可能是由我的新手引起的)是否有更方便的方法来加载shapefile?我觉得一直重命名gadm-Object是非常愚蠢的.也许甚至有一种方法,R只下载一次数据,然后将它们存储在工作区/本地某处?
  2. 我怎样才能说服R在一张地图上绘制所有这些国家?

先感谢您!

[编辑]

一些不错的函数 在Gavin Simpson的帮助下,我能够创建一些很好的函数,将整个地图合并减少到一行:

## you will need the sp-package
library('sp')

## load a file from GADM (you just have to specify the countries "special part" of the file name, like "ARG" for Argentina. Optionally you can specify which level you want to have
loadGADM <- function (fileName, level = 0, ...) {
    load(url(paste("http://biogeo.ucdavis.edu/data/gadm2/R/", fileName, "_adm", level, ".RData", sep     = "")))
    gadm
}

## the maps objects get a prefix (like "ARG_" for Argentina)
changeGADMPrefix <- function (GADM, prefix) {
    GADM <- spChFIDs(GADM, paste(prefix, row.names(GADM), sep = "_"))
    GADM
}

## load file and change prefix
loadChangePrefix <- function (fileName, level = 0, ...) {
    theFile <- loadGADM(fileName, level)
    theFile <- changeGADMPrefix(theFile, fileName)
    theFile
}

## this function creates a SpatialPolygonsDataFrame that contains all maps you specify in "fileNames".
## E.g.: 
## spdf <- getCountries(c("ARG","BOL","CHL"))
## plot(spdf) # should draw a map with Brasil, Argentina and Chile on it.
getCountries <- function (fileNames, level = 0, ...) {
    polygon <- sapply(fileNames, loadChangePrefix, level)
    polyMap <- do.call("rbind", polygon)
    polyMap
}
Run Code Online (Sandbox Code Playgroud)

当您找到此页面时,请务必阅读以下答案:https: //stackoverflow.com/a/33264548/263589

Rob*_*ans 7

对此最简单的解决方案是

library(raster)
bol <- getData('GADM', country='BOL', level=1)

plot(bol)
Run Code Online (Sandbox Code Playgroud)

R数据被添加到GADM网站以支持此功能.另请注意,文件格式已更改,以使此页面上描述的其他功能不再起作用.

结合不同的国家

per <- getData('GADM', country='PER', level=1)
bp <- bind(bol, per)
plot(bp)
Run Code Online (Sandbox Code Playgroud)


Rei*_*son 6

对于问题1,这是R,因此您可以滚动自己的load()功能,执行您想要的功能,例如:

loadGADM <- function(file, ...) {
    load(file, ...)
    gadm
}
Run Code Online (Sandbox Code Playgroud)

并将其用作:

> ls()
character(0)
> loadGADM <- function(file, ...) {
+     load(file, ...)
+     gadm
+ }
> arg <- loadGADM(url('http://gadm.org/data/rda/ARG_adm0.RData'))
> ls()
[1] "arg"      "loadGADM"
Run Code Online (Sandbox Code Playgroud)

当你知道加载的对象将被调用时,这是一个本地解决方案gadm- 你可以改进函数不需要它,例如:

loadGADM <- function(file, ...) {
    f <- load(file, ...)
    get(f)
}
Run Code Online (Sandbox Code Playgroud)

这是有效的,因为load()返回已加载对象的名称的字符串.

对于问题2,您需要rbind()将三个sp对象放在一起,而不是将它们连接起来.但是,这不适用于这些对象,并且多边形ID是非唯一的:

> sa <- rbind(arg, chl, bol)
Error in validObject(res) : 
  invalid class "SpatialPolygons" object: non-unique Polygons ID slot values
Run Code Online (Sandbox Code Playgroud)

我正在研究这个问题,如果我找到解决方法,我会更新.解决方案是使用更改多边形ID槽值spChFIDs().在这里,我们将"arg_"对象附加到对象的rownames,这些并不是唯一的:

arg <- spChFIDs(arg, paste("arg", row.names(arg), sep = "_"))
chl <- spChFIDs(chl, paste("chl", row.names(chl), sep = "_"))
bol <- spChFIDs(bol, paste("bol", row.names(bol), sep = "_"))
sa <- rbind(arg, chl, bol)
Run Code Online (Sandbox Code Playgroud)

然后我们可以绘制组合sp对象:

plot(sa) ## beware might take a long time to plot...
Run Code Online (Sandbox Code Playgroud)