3 min read

Interactive map with leaflet

This post aim to train you how to make an interactive in R. We are going to focus on some poular packages for these task, many are part onf an tidyverse ecosystem (Wickham 2017) with addition of leaflet(Cheng, Karambelkar, and Xie 2018) package. To work with these post you need to have version 3.4 and above of R(R Core Team 2018) installed in your machine. You also need to install the add-on packages leaflet and tidyverse if are not in your machine. We laod the packages we need for this task with require() function.

require(tidyverse)
require(oce)
require(leaflet)
require(RColorBrewer)

Then we load the data from the local directory into R’s workspace with the read_csv() function from readr package (Wickham, Hester, and Francois 2017).

surface = read_csv("./surface_ctd.csv")

Table 1 show the dataset contains six variable—temperature, fluorescence, oxygen all measured at water depth of 5 meter. The dataset also contains geographical (longitude and latitude) information, that we can used for making interactive map.

surface %>%
  select(1:6) %>%
  knitr::kable(format = "html", digits = 2, 
               col.names = c("Pressure", "Temperature", "Fluorescence", "Oxygen", "Longitude", "Latitude"),
               align = "c", 
               caption = "A table showing the variable in the dataset") %>%
  kableExtra::column_spec(column = 1:6, width = "4cm")
Table 1: A table showing the variable in the dataset
Pressure Temperature Fluorescence Oxygen Longitude Latitude
5 26.94 0.36 3.97 39.29 -5.02
5 26.96 0.59 4.01 39.24 -5.26
5 26.90 0.46 3.99 39.19 -5.44
5 27.01 0.40 3.99 39.11 -5.71
5 27.28 0.39 3.90 39.07 -5.93
5 27.32 0.31 3.96 38.99 -6.14
5 27.38 0.32 3.99 39.01 -6.26
5 27.26 0.30 3.99 39.24 -6.49
5 26.71 0.58 3.97 39.35 -6.67

We map the location and show the variation of fluorence in each marker. We need to color the location markers with fluorescence values. To build a colour palette, we can use some utility functions in the leaflet package. The code below create a function that will generate a Yellow–Orange–Red palette from RcolorBrewer (Neuwirth 2014) package. The domain argument ensure that our colour scale will grade from the minimum to maximum fluorescence values.

oranges = colorNumeric(palette = "YlOrRd" ,
                       domain = range(surface$fluorescence))

We can now map the markers and specify the location markers coded with the oranges function of color pallete we just created

leaflet(data = surface) %>%
  addProviderTiles("Esri.OceanBasemap") %>%
  addCircleMarkers(lng = ~lon, 
                   lat = ~lat, 
                   radius = 5, 
                   color = "grey80",  
                   weight = 1, 
                   fill = TRUE, 
                   fillOpacity = 0.7, 
                   fillColor = ~oranges(fluorescence)) 

We further add a legend to tell us what shade of color correspond to which value of fluorescence

leaflet(data = surface) %>%
  addProviderTiles("Esri.OceanBasemap") %>%
  addCircleMarkers(lng = ~lon, 
                   lat = ~lat, 
                   radius = 8, 
                   color = "grey80",  
                   weight = 1, 
                   fill = TRUE, 
                   fillOpacity = 0.7, 
                   fillColor = ~oranges(fluorescence)) %>%
  addLegend(position = "topright", 
            pal = oranges, 
            values = surface$fluorescence, 
            title = "mg/L", 
            opacity = 1)

References

Cheng, Joe, Bhaskar Karambelkar, and Yihui Xie. 2018. Leaflet: Create Interactive Web Maps with the Javascript ’Leaflet’ Library. https://CRAN.R-project.org/package=leaflet.

Neuwirth, Erich. 2014. RColorBrewer: ColorBrewer Palettes. https://CRAN.R-project.org/package=RColorBrewer.

R Core Team. 2018. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

Wickham, Hadley. 2017. Tidyverse: Easily Install and Load the ’Tidyverse’. https://CRAN.R-project.org/package=tidyverse.

Wickham, Hadley, Jim Hester, and Romain Francois. 2017. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.