也許由於我是R相對較新的事實,我在使用http://www.gadm.org/上的gadm-Mapfiles時遇到問題。GADM-Maps跨國比較圖形
我嘗試繪製幾個國家的地圖,並將它們相互比較(使用不同的顏色)。
這是我做的
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"
這裏是我的問題:
- (這個問題可能是由我newbieness引起)是否有裝載形狀文件更方便的方法?我發現它總是重新命名gadm-Object,這很愚蠢。也許甚至有一種方式,R只下載一次數據,然後將它們存儲在工作區/本地某處?
- 我該如何說服R在一張地圖上繪製所有這些國家?
提前致謝!
[編輯]
一些不錯的功能 隨着加文·辛普森的幫助下,我能創造一些不錯的功能,降低了整個地圖合併到一條線路:
## 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
也許這將是更好地使用maptools而不是SP結合?你能幫我怎麼做嗎? – speendo 2011-02-26 12:21:21
1)的原因是在R中,一個對象有一個名字,並且在save()'d時被保存在對象定義的旁邊。該名稱是該對象保存表示形式的一個組成部分,我不認爲你可以用'load()'做任何事情。然而,這種行爲很容易通過你自己的包裝來繞過'load()'。 – 2011-02-26 14:28:05