6 Create interactive map with leaflet package
To learn about leaflet functions: (https://rstudio.github.io/leaflet/)
6.1 Why interactive maps?:
- Static maps are useful for creating figures for reports and presentation. Sometimes, however, you want to interact with your data. You can use the leaflet package for R to overlay your data on top of interactive maps.
- Leaflet supports various elements for the composition of maps such as Map tiles, Markers, Polygons, Lines, Popups, and GeoJSON.
- Create a collaborative work, and share the results of your study, in an attractive way.
6.3 Add the Snowy owl data
addCircleMakers
function to add the data with x (longitude) and y (latitude) axis. Similar thangeom_point
fromggplot
.
6.4 Customize Leaflet Maps
You can customize your leaflet map too. Let’s do the following:
- Add color palette by year with
colorNumeric
andcolorFactor
to set the palette (example of palettes:(https://www.datanovia.com/en/fr/blog/top-palettes-de-couleurs-r-a-connaitre-pour-une-meilleur-visualisation-des-donnees/) - Adjust the point symbology.
- Add custom data-driven popups to your map.
year <- colorNumeric("YlGnBu", domain = Bubo_2011_2021$year)
year <- colorFactor(topo.colors(16), domain = Bubo_2011_2021$year)
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = Bubo_2011_2021,
lng = Bubo_2011_2021$decimalLongitude,
lat = Bubo_2011_2021$decimalLatitude,
fillColor = ~year(year),
fillOpacity = 0.8,
color = "black",
weight = 1,
radius = 5) %>%
addLegend(data = Bubo_2011_2021,
pal = year,
values = ~year,
title = "Year")
6.5 Add Scale bar
- To add a scale bar, need function
addScaleBar
# ---- Mapping: Add Scale Bar ----
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = Bubo_2011_2021,
lng = Bubo_2011_2021$decimalLongitude,
lat = Bubo_2011_2021$decimalLatitude,
fillColor = ~year(year),
fillOpacity = 0.8,
color = "black",
weight = 1,
radius = 5) %>%
addLegend(data = Bubo_2011_2021,
pal = year,
values = ~year,
title = "Year") %>%
addScaleBar(position = "bottomleft")
6.6 Add Popups
- To add popups, you need to use popup in
addCircleMarkers
and set the colum you want to show in your popup (here the localisation of the birds observed)
# ---- Mapping: Add Popups ----
# Popups appear when you click on a point
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = Bubo_2011_2021,
lng = Bubo_2011_2021$decimalLongitude,
lat = Bubo_2011_2021$decimalLatitude,
fillColor = ~year(year),
fillOpacity = 0.8,
color = "black",
weight = 1,
radius = 5,
popup = ~locality) %>%
addLegend(data = Bubo_2011_2021,
pal = year,
values = ~year,
title = "Year") %>%
addScaleBar(position = "bottomleft")
6.7 Customize your popup
- To customize your own popup, you need to use html
- Hello b is bold tex Hello, in bold text
br adds a line break- Then you can specify replace
popup = ~locality
with the name of your popup created (here mypopup)
# ---- Mapping: Add Custom Popups ----
# Custom pop ups need to be written in html!
# <b>Hello</b> b is bold tex Hello, in bold text
# <br> br adds a line break
Bubo_2011_2021 <- Bubo_2011_2021 %>%
mutate(mypopup = paste0(locality, "<br>",
"<b>Individual count: </b>", individualCount, "<br>",
"<b>Event date: </b>", eventDate, "<br>"))
# Now specify the popup as the custom popup we just created
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = Bubo_2011_2021,
lng = Bubo_2011_2021$decimalLongitude,
lat = Bubo_2011_2021$decimalLatitude,
fillColor = ~year(year),
fillOpacity = 0.8,
color = "black",
weight = 1,
radius = 5,
popup = ~mypopup) %>%
addLegend(data = Bubo_2011_2021,
pal = year,
values = ~year,
title = "Year") %>%
addScaleBar(position = "bottomleft")
- If you want to save and share your collaborative map, you can use the function
saveWidget
from the packagehtmlWidgets
.