Графика сравнения между странами GADM-Maps

Возможно, из-за того, что я относительно новичок в R, у меня проблемы с использованием файлов gadm-Mapfiles на http: //www.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"

Вот мои проблемы:

  1. (Этот вопрос, вероятно, вызван моим новичком) Есть ли более удобный способ загрузки шейп-файлов? Я считаю довольно глупым все время переименовывать gadm-Object.

    [править]

    несколько хороших функций С помощью Гэвина Симпсона я смог создать несколько хороших функций, которые сводят все слияние карт к одной строке:

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

10
задан Cœur 8 February 2018 в 19:13
поделиться