Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

r - GADM-Maps cross-country comparison graphics

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:

  1. (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?
  2. 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The simplest solution for this is

library(raster)
bol <- getData('GADM', country='BOL', level=1)

plot(bol)

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

per <- getData('GADM', country='PER', level=1)
bp <- bind(bol, per)
plot(bp)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...