Leaflet is an R package that allows for creating interactive maps. Features include panning and zooming, creating different layers like markers, circles, polygons, and values to those markers with a chloropleth map. Packages needed are:
# install.packages("utils")
# install.packages("tidyverse")
# install.packages("leaflet")
# install.packages("sf")
# install.packages("here")
First, use the read.csv() to read the file in table format 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 several function calls and then assign it to the object “dc”.
# library(utils)
All <- 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()
d1 <- All %>% filter(StudyNumber==1) # %>% https://github.com/tidyverse/magrittr
Assign an object d1.map as the base map by using the leaflet() function to make a Leaflet map widget for the filtered data d1, and use the operators pipe to assign the function addTiles() for the base map.
library(leaflet)
d1.map <- leaflet(d1) %>% addTiles()
d1.map
Add a layer to the base map with the addMarkers() function. Specify the map, the coordinates for each marker, and the title shown when clicking the marker.
addMarkers(map=d1.map, lng= ~Long, lat= ~Lat, popup='Dataset 1 sites')
To fix the jumbled markers, add small circles for each location. Use the addCircle() function to add a layer to the base map by specifying the map, the coordinates for each circle, default color is blue, radius of all circles, weight for line thickness, and the label shown when hovering over each individual circle which is specified as the dataset’s ID number.
# library(leaflet)
d1.map <- leaflet(d1) %>% addTiles()
addCircles(d1.map, lng= ~Long, lat= ~Lat, radius=10, weight=2, label= ~paste0(formatC(d1$ID)))
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
polygon
## 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
## First 10 features:
## stroke stroke.w stroke.o fill fill.opa title
## 1 #555555 2 1 #555555 0 NSA Property No. 129
## 2 #555555 2 1 #555555 0 NSA Property No. 128
## 3 #555555 2 1 #555555 0 NSA Property No. 126
## 4 #555555 2 1 #555555 0 NSA Property No. 127
## 5 #555555 2 1 #555555 0 NSA Property No. 124
## 6 #555555 2 1 #555555 0 NSA Property No. 121
## 7 #555555 2 1 #555555 0 NSA Property No. 120
## 8 #555555 2 1 #555555 0 NSA Property No. 119
## 9 #555555 2 1 #555555 0 NSA Property No. 118
## 10 #555555 2 1 #555555 0 NSA Property No. 117
## geometry
## 1 POLYGON ((-111.1132 33.2867...
## 2 POLYGON ((-111.1124 33.2873...
## 3 POLYGON ((-111.1121 33.2874...
## 4 POLYGON ((-111.1116 33.2869...
## 5 POLYGON ((-111.1116 33.2877...
## 6 POLYGON ((-111.1098 33.2876...
## 7 POLYGON ((-111.1096 33.2879...
## 8 POLYGON ((-111.1096 33.2880...
## 9 POLYGON ((-111.1094 33.2883...
## 10 POLYGON ((-111.1092 33.2886...
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)
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()
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)")