# Author: Egge-Jan Polle - 2 November 2015 - http://www.twiav.nl/
# A short tutorial on using the leaflet package to show multiple map layers in the RStudio Viewer pane
# load the required packages
library(sp)
library(rgdal)
library(leaflet)
# create and set a working directory
dir.create("./RTutorial")
setwd("./RTutorial")
# create a subfolder in the worikng directory to store your data
dir.create("./Data")
# download and unzip the Municipalities dataset
URL <- "http://www.twiav.nl/files/NL_Municipalities2014.zip"
download.file(URL, destfile ="./Data/NL_Municipalities2014.zip", mode = "wb")
unzip("./Data/NL_Municipalities2014.zip", exdir = "./Data")
unlink("./Data/NL_Municipalities2014.zip") # delete zip file
# download and unzip the Railway and Stations datasets
URL <- "http://www.twiav.nl/files/NL_RailwaysAndStations2015.zip"
download.file(URL, destfile ="./Data/NL_RailwaysAndStations2015.zip", mode = "wb")
unzip("./Data/NL_RailwaysAndStations2015.zip", exdir = "./Data")
unlink("./Data/NL_RailwaysAndStations2015.zip") # delete zip file
rm(URL)
# 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")
# have a quick look at the data (optional)
View(nlMun2014)
View(nlStations2015)
View(nlRailways2015)
plot(nlMun2014)
plot(nlStations2015, add=TRUE)
plot(nlRailways2015, add=TRUE)
# 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"))
# create popups
station_popup <- paste0("
")
railway_popup <- paste0("Route: | ",nlRailways2015_wgs84$Route," |
Operator: | ",nlRailways2015_wgs84$Operator," |
")
mun_popup <- paste0("Name: | ",nlMun2014_wgs84$Name," |
Province: | ",nlMun2014_wgs84$Province_name," |
Inhabitants: | ",format(nlMun2014_wgs84$Inhabitants, big.mark = ".", decimal.mark = ",")," |
")
# 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)
)
# show the interactive map
imap