Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4016912
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:47:51+00:00 2026-05-20T09:47:51+00:00

Maybe due to the fact I’m relatively new to R, I have problems using

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:47:52+00:00Added an answer on May 20, 2026 at 9:47 am

    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
    }
    

    And use it as:

    > ls()
    character(0)
    > loadGADM <- function(file, ...) {
    +     load(file, ...)
    +     gadm
    + }
    > arg <- loadGADM(url('http://gadm.org/data/rda/ARG_adm0.RData'))
    > ls()
    [1] "arg"      "loadGADM"
    

    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)
    }
    

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

    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
    

    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)
    

    Then we can plot the combined sp object:

    plot(sa) ## beware might take a long time to plot...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Maybe I just don't know .NET well enough yet, but I have yet to
Maybe world peace would be easier, but I have a problem with the widths
Maybe this is a dumb question, but I have the following behavior in Visual
This snippet throws an NullPointerException due to the fact that its unboxed to a
I am having a design issue, this may be due to the fact that
I am looking into creating a new website using ASP.NET 4.0. I am currently
I understand one of the (maybe best) ways of using inversion of control is
Maybe the need to do this is a 'design smell' but thinking about another
Maybe this is a dumb question, but is there any way to convert a
Maybe I'm just thinking about this too hard, but I'm having a problem figuring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.