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 packages 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.)

Map of Europe by ggmap

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.

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.

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:

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

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.

View (and download) the full code:

Related Post