# Maps in R: Introduction – Drawing the map of Europe

December 18, 2012
By

(This article was first published on Milano R net, and kindly contributed to R-bloggers)

This post is a brief follow-up to a question that appeared some time ago on the “The R Project for Statistical Computing” LinkedIn group, which I’m reporting here:

### How can I draw a map of MODERN Europe?

Hi, I'm trying to draw a map of modern Europe but I've found only maps of twenty years ago, with Yugoslavia and Czechoslovakia still united!!!
Does anyone know where I can get a more recent map to be employed with packagess such as 'sp' or 'maps'?
Thank you very much!

Two different solutions to the above question will be provided here, using two different R packages.

# Solution #1 - ggmap

The package ggmap allows visualizations of spatial data on maps retrieved from Google Maps, OpenStreetMap or other services.

A map of Europe is obtained with just four lines of R code (including the loading of packages.)

 > library(ggmap) > library(mapproj) > map <- get_map(location = 'Europe', zoom = 4) > ggmap(map)

The second line gets the map from Google Maps, unless a different source parameter is specified (for example source = "osm" for retrieving the map from OpenStreetMap.)

The argument location requires the text you would type into Google Maps search box if you were looking to obtain your map online.

The zoom argument requires an integer. Zero would give a map of the whole world, while the max value of 21 returns a map to the building detail. I chose a value of 4 to display Europe after some trial and error process.

The final line is the one actually drawing the map.

Note that the object returned by ggmap is of class ggplot, thus anything you would normally do with a ggplot (like adding geometries) can be done with ggmap.

# Solution #2 – rworldmap

The map produced by the ggmap package is a raster. In other words it is just an image placed on the R graphic device. While it serves the purpose of displaying Europe, and would be fine for the display of spatial point patterns, the raster map is not very convenient when one has data aggregated at the country level and wants to show them by color-coding each country.

Package rworldmap is better suited for the task, as it provides maps as spatial polygons.

Again, plotting a map is just a matter of three lines of code, package loading included.

 > library(rworldmap) > newmap <- getMap(resolution = "low") > plot(newmap)

The code is very similar to the one of ggmap. The resolution argument is quite self-explanatory and you can see from the resulting map that  "low" is actually a more than acceptable resolution.

In this case we got a map of the whole world. By simply tinkering with the xlim and ylim arguments of the plot function we can limit the display to just Europe.

 > plot(newmap, > xlim = c(-20, 59), > ylim = c(35, 71), > asp = 1 > )

Admittedly I got the right numbers for the xlim and ylim parameters with some trial and error process this time too.

Would it be possible to retrieve them in a more automated way?

# A quick look at geocoding

Wikipedia has the this entry on the extreme points of Europe.

The geocode function from the ggmap package finds the coordinates of a location using Google Maps. Thus, finding the coordinates of the European most extreme points is as easy as typing the following code:

 > library(ggmap) > europe.limits <- geocode(c("CapeFligely,RudolfIsland,Franz Josef Land,Russia", > "Gavdos,Greece", > "Faja Grande,Azores", > "SevernyIsland,Novaya Zemlya,Russia") > ) > europe.limits         lon      lat 1  54.78333 80.56667 2  24.08464 34.83469 3 -31.26192 39.45479 4  59.34569 62.21215

The xlim and ylim arguments passed to the plot function in the previous section can slightly be modified like this:

 > plot(newmap, > xlim = range(europe.limits$lon), > ylim = range(europe.limits$lat), > asp = 1 > )

# What’s next?

As R users we hardly need a map that does not feature any data, thus in future posts we will have a look at how to visualize both spatial point patterns and spatially aggregated data on maps.

We will also provide sources to retrieve spatial polygons for different levels of geographical entities, such as regions for example.

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28  library(ggmap) library(mapproj) map <- get_map(location = 'Europe', zoom = 4) ggmap(map)   library(rworldmap) newmap <- getMap(resolution = "low") plot(newmap)   plot(newmap, xlim = c(-20, 59), ylim = c(35, 71), asp = 1 )   library(ggmap) europe.limits <- geocode(c("CapeFligely,RudolfIsland,Franz Josef Land,Russia", "Gavdos,Greece", "Faja Grande,Azores", "SevernyIsland,Novaya Zemlya,Russia") ) europe.limits   plot(newmap, xlim = range(europe.limits$lon), ylim = range(europe.limits$lat), asp = 1 )