GADM-Maps 跨国比较图
也许由于我对 R 比较陌生,我在 http://www 上使用 gadm-Mapfiles 时遇到问题.gadm.org/。
我尝试绘制几个国家的地图并将它们相互比较(使用不同的颜色)。
这就是我所做的
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"
这是我的问题:(
- 这个问题可能是由我的新手引起的)是否有更方便的方法来加载形状文件?我发现一直重命名 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
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).
This is what I do
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"
Here are my problems:
- (This question is probably caused by my newbieness) Is there a more convenient way to load the shapefiles? I find it quite stupid to rename the gadm-Object all the time. Maybe there is even a way where R only downloads the data once and then has them stored in the workspace/somewhere locally?
- How can I convince R to plot all those countries on ONE map?
Thank you in advance!
[edit]
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
}
When you find this page, make sure you read this answer:
https://stackoverflow.com/a/33264548/263589
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的解决方案是
将 R 数据添加到 GADM 网站以支持此功能。另请注意,文件格式已更改,因此本页描述的其他功能不再起作用。
将不同国家结合起来
The simplest solution for this is
The R data were added to the GADM website to support this function. Also note that the file format has changed such that the other functions described on this page no longer work.
To combine different countries
对于问题 1,这是 R,因此您可以推出自己的
load()
函数来执行您想要的操作,例如:并将其用作
:当您知道对象已加载时,这是一个本地解决方案将被称为
gadm
- 您可以改进该函数以不需要它,例如:它可以工作,因为
load()
返回已加载对象名称的字符串。对于问题 2,您需要将三个
sp
对象rbind()
放在一起,而不是连接它们。但是,这对这些对象不起作用,并且多边形 ID 不是唯一的:我正在解决此问题,如果我找到解决方法,将会更新。解决方案是更改多边形使用spChFIDs()
获取 ID 槽值。在这里,我们将"arg_"
等附加到对象的行名称中,这样它们并不都是唯一的:然后我们可以绘制组合的
sp
对象:For problem 1, this is R so you can roll your own
load()
function that does what you want, for example:And use it as:
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.:which works because
load()
returns the character strings of the names of the loaded objects.For problem 2, you need to
rbind()
the threesp
objects together, not concatenate them. However, this doesn't work for these objects and the Polygon IDs are non-unique: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 usingspChFIDs()
. Here we append"arg_"
etc to the rownames of the the objects such that these are no all unique:Then we can plot the combined
sp
object:我可以提供一些小的改进。
首先,如果您想绘制根据某些标准对国家/地区进行着色的地图图形,则必须在相应的 GADM 文件中加载该值,例如从另一个 data.frame 加载该值。我是这样做的:
如果您绘制较大区域的地图,您的地图将会扭曲。使用 rgdal 包(及其依赖项)应用地图投影:
为了绘制属性 coef,sp 包提供了一个很好的方法方法:
There some minor improvements that I can offer.
First, if you want to draw map graphics which shade countries according to some criteria, you have to load that value in the respective GADM file, e.g. from another data.frame. Here is how I did it:
If you draw maps of larger areas, your maps will suffer from distortion. Use the
rgdal
package (and its dependencies) to apply a map projection:In order to plot the property
coef
, the packagesp
offers a nice method: