Navigation
Appearance

Geographic maps in R

DavidMSmith21 Oct 2009 · 1 min read
Geographic maps in R
Illustration generated with FLUX.2 [klein 4B] via Cloudflare Workers AI.

The maps library for R is a powerful tool for creating maps of countries and regions of the world. For example, you can create a map of the USA and its states in just three lines of code:

See more SmartData Collective in your Google results.Add as sourceFollow

library(maps)

map(“state”, interior = FALSE)

map(“state”, boundary = FALSE, col=“gray”, add = TRUE)

USA map

The coordinate system of the graph is latitude and longitude, so it’s easy to overlay other spatial data on this map.

Unfortunately, the data for the maps library isn’t sufficient for some applications. The maps themselves are fairly low-resolution (although higher-resolution data is available in the mapdata package), and political boundaries can be incomplete or out-of-date. Luckily, there are now free online resources where you can find updated map data for use with R. 

GADM is a spatial database of the location of the world’s administrative boundaries, and as Claudia Engel discovered the map information is available as native R objects that can be plotted directly with the spplot function (from the sp package). For example, here’s how to load the data for Switzerland, and then plot each canton with a color denoting its primary language:

library(sp)
con <- url(“http://gadm.org/data/rda/CHE_adm1.RData”)
print(load(con))
close(con)

language <- c(“german”, “german”, “german”,“german”,
 “german”,“german”,“french”, “french”,
 “german”,“german”,“french”, “french”, 
 “german”, “french”,“german”,“german”,
 “german”,“german”,“german”, “german”,
 “german”,“italian”,“german”,“french”,
 “french”,“german”,“german”)
gadm$language <- as.factor(language)
col = rainbow(length(levels(gadm$language)))
spplot(gadm, “language”, col.regions=col, main=“Swiss Language Regions”)

    Swiss_lang
    Sweet!

    AnthroSpace: Download Global Administrative Areas as RData files


    Filed underr language
    Share this article

    More on data visualization

    Find a perspective.

    Saved for later.

    Stored on this device only. Nothing is sent to SmartData.

      Nothing saved yet. Use “Save” on any article to keep it here.