# Load the Leaflet library - only need to do this once
library(leaflet)
#1
#create an empty canvas and assign it to the variable m, display m, ie. show the map
m <- leaflet()
m
#2
# add a default tile ( the whole world) to the canvas
# the %>% is the pipe operator, it allows instructions to be chained together
m <- leaflet() %>%
addTiles()
m
#3
# set a center for the map and an initial zoom level
m <- leaflet() %>%
addTiles() %>%
setView(0, 51.478792, zoom = 12)
m
#4
# add a marker to the map and provide some txt for it
popup_val <- "Hello Greenwich"
m <- leaflet() %>%
addTiles() %>%
setView(0, 51.478792, zoom = 15) %>%
addMarkers(lng=0, lat=51.478792, popup=popup_val)
m
#5
# the text can be HTML which will be interpreted
# in this case the text is in bold and there is a link to a web page
popup_val <- "Hello Greenwich
All about GMT"
m <- leaflet() %>%
addTiles() %>%
setView(0, 51.478792, zoom = 15) %>%
addMarkers(lng=0, lat=51.478792, popup=popup_val)
m
#6 now we want to place many markers by reading the data from a file
# and passing the dataframe into the addMarkers function
# because the contents of the PC column is just text, we just see the contents.
# Windows file location is C:\sa_data\Aberdeen_PC.csv
aberdeen <- read.csv("C:/sa_data/Aberdeen_PC.csv")
#aberdeen <- read.csv("C:\\sa_data\\Aberdeen_PC.csv")
m <- leaflet() %>%
addTiles() %>%
setView(-2.096647861, 57.14822809, zoom = 12) %>%
addMarkers(data = aberdeen, lng = ~ Long, lat = ~ Lat, popup = aberdeen$PC)
m
#7
# quite often we want several items of info in the popup
# we can do this by createing an HTML table structure and imbedding the required info.
# in this example we have hand coded the data
popup_val <- paste0("
Post Code |
AB10 1AA |
Name |
George St/Harbour Ward |
Admin Code |
S12000033 |
")
m <- leaflet() %>%
addTiles() %>%
setView(-2.096647861, 57.14822809, zoom = 15) %>%
addMarkers(lng=-2.096647861, lat=57.14822809, popup=popup_val)
m
#8
# we can do something similar, but using the data read from the file
# in this case combining 3 different columns from the dataframe
m <- leaflet() %>%
addTiles() %>%
setView(-2.096647861, 57.14822809, zoom = 15) %>%
addMarkers(data = aberdeen, lng = ~ Long, lat = ~ Lat,
popup = paste0("
Post Code |
", aberdeen$PC, " |
Name |
", aberdeen$Name, " |
Admin Code |
", aberdeen$Admin, " |
"))
m
#9
# the last bit of coding looks a bit messy.
# As an alternative we could create a new column in the dataframe to hold
# the structure and content of the HTML table for the popups.
# this makes the addMarkers call clearer, but bear in mind, you might
# be considerabley be adding to the size of the dataframe.
aberdeen$popup <- paste0("
Post Code |
", aberdeen$PC, " |
Name |
", aberdeen$Name, " |
Admin Code |
", aberdeen$Admin, " |
")
m <- leaflet() %>%
addTiles() %>%
setView(-2.096647861, 57.14822809, zoom = 15) %>%
addMarkers(data = aberdeen, lng = ~ Long, lat = ~ Lat,
popup = ~ popup)
m
#10
# finally, you will have noticed the problem of trying to plot too many
# markers on the map.
# As an alternative to the addMarkers function, there is the AddCircleMarkers function
# This will group the markers, and automatically split them as you zoom in on the map.
m <- leaflet() %>%
addTiles() %>%
setView(-2.096647861, 57.14822809, zoom = 15) %>%
addCircleMarkers(data = aberdeen, lng = ~ Long, lat = ~ Lat, radius = 5,
clusterOptions = markerClusterOptions(), popup = ~ popup)
m