Geographic maps in R

3 Min Read

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:

library(maps)

map(“state”, interior = FALSE)

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

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 … 



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:

library(maps)

map(“state”, interior = FALSE)

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

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


Sweet!

AnthroSpace: Download Global Administrative Areas as RData files

Link to original post

TAGGED:
Share This Article
Exit mobile version