也许是由于我对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"


这是我的问题:


(此问题可能是由我的新手引起的)是否有更方便的方式来加载shapefile?我发现总是重命名gadm-Object很愚蠢。也许甚至有一种方法,其中R仅下载一次数据,然后将其存储在工作区/本地某个地方?
我如何说服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
}


找到此页面时,请确保阅读以下答案:
https://stackoverflow.com/a/33264548/263589

最佳答案

对于问题1,它是R,因此您可以滚动自己的load()函数来执行所需的操作,例如:

loadGADM <- function(file, ...) {
    load(file, ...)
    gadm
}


并将其用作:

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


当您知道加载的对象将被称为gadm时,这是一种本地解决方案-您可以改进功能以使其不需要此信息,例如:

loadGADM <- function(file, ...) {
    f <- load(file, ...)
    get(f)
}


之所以起作用,是因为load()返回已加载对象名称的字符串。

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

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


我正在研究这个问题,如果我能解决的话会进行更新。解决方法是使用spChFIDs()更改“多边形ID”插槽值。在这里,我们在对象的行名后附加"arg_"等,以使它们并非唯一:

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)


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

plot(sa) ## beware might take a long time to plot...

07-26 05:15