Introduction

In this tutorial we will show you how to create an interactive map in the RStudio Viewer pane using the R package leaflet.

It is exactly the map in the window above that we are going to recreate in this exercise. The map contains three layers with attribute data. Please feel free to click on the stations (points), railways (lines) and municipalities (polygons) to see their respective popup windows. Of course you can also zoom in, zoom out and pan around. It is an interactive map after all.

You can also change the basemap from OpenStreetMap to one of the Stamen layers. This watercolor one is nice, isn’t it?

The full script to create this map - including the download of the relevant data - can be found here: InteractiveMapWindowInRStudio.R. It is recommended to run this script line by line. In this way you will be able to appreciate the result of each step. Below we will have a closer look at the different steps.

R or RStudio

You can run the script either in R or in RStudio. RStudio includes a Viewer pane that can be used to view local web content, in our case the interactive map. While R itself does not have such a Viewer pane, it will open the leaflet object immediately in your web browser.

Apart from leaflet, you will also need the packages sp and rgdal.

Data

The data used in this exercise is hosted on this very same website, on the page Geographic data. The metadata of the tables used can be found via the links below:

Please do not download and unzip the data manually - the R script contains lines of code to do this for you, see the functions download.file() and unzip(). With unlink() we delete the downloaded zip files when we do not need them anymore (just to save some disk space).

Now we can read in the data:

# read the data
nlMun2014 <- readOGR("Data/NL_Municipalities2014.TAB", "NL_Municipalities2014")
nlStations2015 <- readOGR("Data/NL_Stations2015.TAB", "NL_Stations2015")
nlRailways2015 <- readOGR("Data/NL_Railways2015.TAB", "NL_Railways2015")

Reprojection

As stated in the metadata, the tables used are provided in some local projection system, the Netherlands National System (EPSG:28992). To be able to show them on top of the OpenStreetMap we have to reproject them to WGS84 (EPSG:4326):

# reproject the data to wgs84
nlRailways2015_wgs84 <- spTransform(nlRailways2015, CRS("+init=epsg:4326"))
nlStations2015_wgs84 <- spTransform(nlStations2015, CRS("+init=epsg:4326"))
nlMun2014_wgs84 <- spTransform(nlMun2014, CRS("+init=epsg:4326"))

Attribute data in poup

To create nice popups to show attribute data in the map viewer you will need some basic knowledge of the HyperText Markup Language (HTML).

This line of code will create a popup string for each of the 398 railway stations in our dataset:

station_popup <- paste0("<table><tr><td bgcolor=#E0E0E0><strong>Station: </strong></td><td><a href=",nlStations2015_wgs84$Wikipedia,">",nlStations2015_wgs84$Station,"</a></td></tr></table>")

Please note: we link to Wikipedia pages about the railway stations in the Dutch language. For most stations there is also an English page available. We might update the dataset one day with direct links to these English pages.

This line of code will create a popup string for each of the 115 railway lines in our dataset:

railway_popup <- paste0("<table><tr><td bgcolor=#E0E0E0><strong>Route: </strong></td><td>",nlRailways2015_wgs84$Route,"</td></tr><tr><td bgcolor=#E0E0E0><strong>Operator: </strong></td><td>",nlRailways2015_wgs84$Operator,"</td></tr></table>")

The municipal dataset contains 30+ columns with attribute data, which we do not want to show all in the popup. So we have made a selection of the columns:

mun_popup <- paste0("<table><tr><td bgcolor=#E0E0E0><strong>Name: </strong></td><td>",nlMun2014_wgs84$Name,"</td></tr><tr><td bgcolor=#E0E0E0><strong>Province: </strong></td><td>",nlMun2014_wgs84$Province_name,"</td></tr><tr><td bgcolor=#E0E0E0><strong>Inhabitants: </strong></td><td>",format(nlMun2014_wgs84$Inhabitants, big.mark = ".", decimal.mark = ","),"</td></tr></table>")

Please note: we format the numbers with a “.” as big.mark (thousands separator), because in mainland Europe the “,” is used as decimal sign. You can modify this, depending on your target audience.

Interactive map

Now we are ready to create our interactive map:

# create the interactive map
imap <- leaflet() %>%
  # Base groups
  addTiles(group = "Openstreetmap") %>%
  addProviderTiles("Stamen.TonerLite", group = "Stamen Toner Lite") %>%
  addProviderTiles("Stamen.Watercolor", group = "Stamen Watercolor") %>%
  setView(lng = 4.9, lat = 52.378333, zoom = 12) %>% # Amsterdam Centraal Railway Station
  # Overlay groups
  addPolygons(
    data=nlMun2014_wgs84,
    group = "Municipalities (2014)",
    popup = mun_popup,
    fillOpacity = 0,
    color = "#900000",
    weight = 2
  ) %>%
  addPolylines(
    data=nlRailways2015_wgs84,
    group = "Railways",
    popup = railway_popup,
    opacity = 1
  ) %>%
  addCircles(
    data=nlStations2015_wgs84,
    group = "Stations",
    popup = station_popup,
    weight = 3,
    radius=80,
    color="#FF0000",
    stroke = TRUE,
    fillOpacity = 0.8
  ) %>%
  # Layers control
  addLayersControl(
    baseGroups = c("Openstreetmap", "Stamen Toner Lite", "Stamen Watercolor"),
    overlayGroups = c("Stations", "Railways", "Municipalities (2014)"),
    options = layersControlOptions(collapsed = FALSE)
  )

And now you can show it:

# show the interactive map
imap

This will give you exactly the map shown at the top of this page.



Egge-Jan Pollé