GADM-Maps 越野对比图 [英] GADM-Maps cross-country comparison graphics

查看:15
本文介绍了GADM-Maps 越野对比图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许由于我对 R 比较陌生,我在使用 http 上的 gadm-Mapfiles 时遇到问题://www.gadm.org/.

Maybe due to the fact I'm relatively new to R, I have problems using the gadm-Mapfiles on http://www.gadm.org/.

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

I try to draw a map with several countries and compare them to each other (using different colors).

这就是我所做的

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"

这是我的问题:

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

先谢谢你!

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

some nice functions With the help of Gavin Simpson, I was able to create some nice functions that reduce the whole map-merging to one line:

## 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

When you find this page, make sure you read this answer: https://stackoverflow.com/a/33264548/263589

推荐答案

对于问题 1,这是 R,因此您可以推出自己的 load() 函数来执行您想要的操作,例如:

For problem 1, this is R so you can roll your own load() function that does what you want, for example:

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 时,这是一个本地解决方案 - 您可以改进该功能以使其不需要此功能,例如:

This is a local solution when you know that the object loaded will be called gadm - you could improve the function to not need this, e.g.:

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

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

which works because load() returns the character strings of the names of the loaded objects.

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

For problem 2, you need to rbind() the three sp objects together, not concatenate them. However, this doesn't work for these objects and the Polygon IDs are non-unique:

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

我正在解决这个问题,如果我找到解决办法,我会更新. 解决方案是使用 spChFIDs() 更改多边形 ID 槽值.在这里,我们将 "arg_" 等附加到对象的行名中,这样它们就不是唯一的了:

I'm working on this and will update if I figure out the work around. The solution is to change the Polygons ID slot values using spChFIDs(). Here we append "arg_" etc to the rownames of the the objects such that these are no all unique:

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对象:

Then we can plot the combined sp object:

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

这篇关于GADM-Maps 越野对比图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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