Leaflet maps

Leaflet is an R package that allows for creating interactive maps. Features include panning and zooming, creating different layers like markers, circles, polygons, and attaching values to those geographic locations. Packages needed are:

# install.packages("utils")
# install.packages("tidyverse")
# install.packages("leaflet")
# install.packages("sf")
# install.packages("here")

First, load the spreadsheet of location and soil values. Use the read.csv() to read the file to create a data frame from it. Tell the function that “NA” should be recognized as “NA”. To filter out only the publicly available data, use the “operators pipe %>%” to couple the filter function and assign it to the object “d1”.

All <- utils::read.csv('SoilFinal.csv', na.strings="NA", header = T) 

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(magrittr)
## 
## Attaching package: 'magrittr'
## The following object is masked from 'package:purrr':
## 
##     set_names
## The following object is masked from 'package:tidyr':
## 
##     extract
d1 <- All %>% filter(StudyNumber==1) # %>% https://github.com/tidyverse/magrittr

use the leaflet() function to make a Leaflet map widget for the filtered data, and use the operators pipe to assign the function addTiles() for the base map.

library(leaflet)
d1.map <- leaflet(d1) %>% addTiles()
d1.map

To visualize the variable of interest across a geographic area, a chloropleth map uses a drawn region (property) and its respective variable intensity (metal concentration). To create a chloropleth map, first find the shapefile that we want to read by the here() function and read it with the st_read() function. This function retrieves layer names and their geometry from the file.

# library(sf)
library(here)
## here() starts at C:/Users/ual-laptop/Desktop/ENVS 567_R
polygon <- here("C:/Users/ual-laptop/Desktop/ENVS 567_R/layers/POLYGON.shp") %>% sf::st_read()
## Reading layer `POLYGON' from data source 
##   `C:\Users\ual-laptop\Desktop\ENVS 567_R\layers\POLYGON.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 258 features and 6 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -111.1132 ymin: 33.28637 xmax: -111.1007 ymax: 33.29823
## Geodetic CRS:  WGS 84

Just like previously, use the leaflet() function to make a Leaflet map widget now with the polygon shapefile. Then, use the operators pipe to assign the function addTiles() for the base map, and then the addPolygons() function with all the defaults.

leaflet(polygon) %>% addTiles() %>%
  addPolygons()

To add color to the polygons by the metal concentration, define the bins with a numeric vector. Cannot have more than 9 bins. Now, call the colorBin() function to color our bins, and specify the pallet which is yellow-orange-red, the domain (i.e., the values being mapped), and the bins.

bins<-c(0,10,20,30,40,50,60,70,80,Inf)

cal<-colorBin(palette="YlOrRd", domain=d1$As..mg.kg., bins=bins)

Customize the interactive map: polygon fill color is dependent on the bincolor palette “cal” function dependent on the metal concentration value, fill opacity=1 is opaque, take away the polygon lines by stroke=FALSE, 0.3 smoothfactor to have the polylines be more accurate when zooming, and have the hover label show the unique “property number” with its corresponding “metal concentration value” (i.e., label= tilde one or more objects that need to be converted to character vectors(title of the geometry in the shp file, “:”, the numeric value given for the location in the csv file.

m<-leaflet(polygon) %>% addTiles() %>%
  addPolygons(fillColor = ~cal(d1$As..mg.kg), 
              fillOpacity = 1, 
              stroke=FALSE, 
              smoothFactor = 0.3, 
    label = ~paste0(title, ": ", 
    formatC(d1$As..mg.kg)))
m

Add a legend to map m. Specify, pal= our cal created pallet, values= the location specific metal concentration, opacity= full 1, title= manually written in ““.

m %>% addLegend(pal = cal, 
      values = ~d1$As..mg.kg., 
      opacity = 1, 
      title="Metal Concentration [X] (mg/kg)")