Réaliser des cartes avec R et OpenStreetMapPlotting maps with R and OpenStreetMap

This tutorial is dedicated to Tibo who showed me how to use OpenStreetMap with R in the first place and gratefully sent me his script. The tutorial will explain first how to install the package OpenStreetMap under linux (which is actually a bit tricky) and will then explain how to use it to draw a map with the open online map OpenStreetMap through R.

Installing the package OpenStreetMap in R

I am using R version 3.0.1 (2013-05-16) — “Good Sport” on Ubuntu 12.04. The R version is thus not that of the ubuntu repository. To install the package OpenStreetMap, I proceeded as follows:

  • first, I installed a few additional ubuntu packages:
    sudo apt-get install libproj-dev libgdal1-dev
  • then, I installed open jdk version 7:
    sudo apt-get remove --purge openjdk-6-*
    sudo apt-get autoremove
    audo apt-get install openjdk-7-*

    and updated R java configuration:

    sudo R CMD javareconf
  • finally, I was able to install the R package OpenStreetMap from R

    (which requires the packages rJava and rgdal that had caused errors before I did the previously described configurations).

Using OpenStreetMap

As an example, I tried to produce a map where the last conferences I gave are located. To do so:

  • the first step is to download the map from OpenStreetMap (or another open map server). This is done by first finding the coordinates of the upper left corner and of the lower right corner on the map (on OpenStreetMap, you can use the “export” menu):
    conf.map = openmap(c(60,-90),c(-45,45),type="osm")
  • the second step is to find the coordinates of the conference places. I give the example below for the year 2013 (Toulouse, Tenerife, Bruges, Lyon). To do so, you can once again use the “export” menu and then the following command lines in R
    coord.conferences = rbind(c(-16.5,28.4),c(3.2,51.2),c(-70.6,-33.4),c(4.8,45.7))
  • then, the coordinates have to be properly converted into spatial points with proper Coordinate Reference System:
    proj4string(coord.conferences) = CRS("+proj=longlat +ellps=WGS84")
    sp.conferences = spTransform(coord.conferences,CRS("+proj=merc +a=6378137
      +b=6378137 +lat_ts=0.0+lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m
      +nadgrids=@null +no_defs"))
  • then the map and the points are plotted:
    sapply(1:nrow(sp.conferences), function(ind) plot(xyb[ind,],add=TRUE,col="blue",
  • and finally, the labels of the points are added:
    sp.conferences.coord = coordinates(sp.conferences)
    conferences.names = c("IWANN","JdS, EGC","ESANN","Rencontres R")
    sapply(1:nrow(sp.conferences.coord), function(ind)

After having added a few more conferences/seminars, I finally obtained the following map:

Another example

The map of the main places for the Journées de Statistique de Toulouse:

has been made with the following command lines:

## Made by Thibault Laurent (for http://jd2013.sfds.asso.fr)
# http://www.tse-fr.eu/index.php?option=com_wrapper&Itemid=398


## Select the bounding box of the map from the upper left corner to the lower
# right corner
## Then select the type of map, from one of those:

type.map=c("osm", "osm-bw", "maptoolkit-topo", "waze", "mapquest",
"mapquest-aerial", "bing", "stamen-toner", "stamen-terrain",
"stamen-watercolor", "osm-german", "osm-wanderreitkarte", "mapbox", "esri",
"esri-topo", "nps", "apple-iphoto", "skobbler", "cloudmade-", "hillshade",
"opencyclemap", "osm-transport", "osm-public-transport", "osm-bbike",

map=openmap(c(43.611522,1.428533), c(43.598903,1.461059),type=type.map[1])

## Locate the main places of the conference
# Create a spatial object and define the coordinate reference system
proj4string(xya)=CRS("+proj=longlat +ellps=WGS84")
xyb=spTransform(xya,CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0
+lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"))

## Plot the map and the points

## Add text
names.xy=c("ESC Toulouse \n (lieu de la conférence)",
"Mairie de Toulouse \n Salle des illustres, \n entrée par la cour Henri IV
(réception le lundi)",
"Médiathèque José-Cabanis \n Espaces Vanel \n (repas de gala le mercredi)",
"Librairie Mona Lisait \n Rencontre avec Guy Limone \n (jeudi 30 mai)",
"Café chez ta mère \n Rencontre des jeunes statisticiens
(café de la statistique le jeudi)",
"Donjon du Capitole \n Départ de la visite de Toulouse \n(mercredi 28 mai)",
"Place d'Arménie \n(retour des visites \n du mercredi)")