R User Group of Milano (Italy)

Maps in R: Introduction - Drawing the map of Europe

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:

13 comments

  1. griffilo /

    I can see this as being very useful, but couldn't get ggmap examples to work. I couldn't find either get_map or geocode functions anywhere?

  2. Hey Max!
    Would be super awesome if you could cover maps and mapping for the different NUTS levels! I know you are conversant in this realm as you seem to work for government.
    And please check out my article on a mysterious increase of deaths caused by viral hepatitis in Germany 1998. There is a chance that you find this increase also in Italy. I'd like to hear your opinion about it!
    Cheers

  3. Mika4real /

    Hi Max Marchi. I have the same problem as griffilo. I've installed all the required packages and I got this error in solution #1:
    Error: could not find function "get_map"

    Can you help me?
    TY

  4. Max Marchi /

    I suggest everyone who is having griffilo's (and Mika4real's) issues, to type sessionInfo() in the R console and copy/paste the resulting information here.
    Hopefully we'll be able to help out.

  5. And even better, and almost just as easy, is to use the vector data from Natural Earth. These are free (license: public domain), detailed, available in various scales, of very high quality and up-to-date. Use the readOGR() function in the rgdal package to load the map data into R.

    One very important thing to keep in mind when plotting maps is that you have to use an appropriate projection (and don’t fiddle with the aspect ratio manually). For Europe, use either the equal-area ETRS-LAEA (EPSG 3035) projection or the conformal ETRS-LCC (EPSG 3034) projection. It’s extremely easy to reproject map data in R. Example:

    mymap.unpro=readOGR(…) # Read in (unprojected) map data
    pro=CRS("+init=epsg:3035") # The projection to use
    mymap.pro=spTransform(bb.pro, pro) # Reproject the map

    For more about the two projections mentioned above, see the EU report Map Projections for Europe.

    • Hi huftis,

      are the stamen maps projected differently from the rworldmap? I don't seem to be able to place my data at the appropriate place on the stamen maps.

  6. Thank you for the post.

    Would you please explain how can I overlap a plot (with color) on the map? I would like to overlay Cincinnati's crime data on the map. The data is structured into cells of of width 0.002x0.002 (lon,lat) and each cell contains number of crimes happened in that place. How can I show it as a heat map (or something similar) while the underlying can still be seen?

    Thanks

    • Max Marchi /

      Next post has examples of plotting data over the map (including transparency options to have the underlying still visible.)
      Hope that one will answer your question, otherwise I'll be happy to provide more help/links.

  7. Hi Max. Thank you for the post.

    I was wondering if it is important to add scale bar or compass rose to the map, and if so, if there is any way to do it with the ggmap package.

    Thank you

Trackbacks/Pingbacks

  1. Maps in R: Plotting data points on a map « Another Word For It - [...] the post: In the introductory post of this series I showed how to plot empty maps in [...]
  2. Map | m's R Blog - [...] journal. Applications: Napa Valley wine tasting map, World map, Europe map, additional [...]

Leave a Reply