Base Map Preparation for the Html leaflet widget

### *Load the leaflet library*

library(leaflet)
library(here)
library(readr)
library(tidyverse)
library(htmlwidgets)

Select the appropriate base map based on the purpose of the study.

### *Call up leaflet() function* and use the pipe (%>%) function to add tiles

leaflet() %>%
  
  #### *Add tiles()* 
  
  addProviderTiles("Esri.WorldImagery")

Figure 1. Esri’s World Imagery base map

But where do I want to focus the leaflet map view? (Figure 2).

#### *Center a map view along the west coast of North America*

leaflet(options = leafletOptions(dragging = TRUE,
                                 minZoom = 1, maxZoom = 3)) %>%
  addProviderTiles("Esri.WorldImagery") %>%
  setView(lng = -118.2437, lat = 34.0522, zoom = 3)

Figure 2. Western North America showing the United States, Canada, Mexico, Central America and the north coast of South America (Esri World Imagery).

#### *Read in the earthquakes greater than 4.6 magnitude data and view the head*

quakes_greater4.5 <- read_csv(here("data","4.5_month.csv"))

head(quakes_greater4.5)
## # A tibble: 6 × 22
##   time                latitude longitude depth   mag magType   nst   gap  dmin
##   <dttm>                 <dbl>     <dbl> <dbl> <dbl> <chr>   <dbl> <dbl> <dbl>
## 1 2023-06-09 22:46:20    16.5      146.  145.    4.5 mb         51    80  1.21
## 2 2023-06-09 22:25:00    -2.81     129.   10     4.7 mb         43    43  1.83
## 3 2023-06-09 21:33:05   -16.7     -174.   92.8   4.8 mb         49    49  4.08
## 4 2023-06-09 21:21:42   -60.5      160.   10     5.9 mww        63    56  6.06
## 5 2023-06-09 21:04:10   -23.3      171.   10     4.8 mb         28    52  3.02
## 6 2023-06-09 20:43:28   -47.3      -12.7  10     5.5 mb        111    60 16.5 
## # ℹ 13 more variables: rms <dbl>, net <chr>, id <chr>, updated <dttm>,
## #   place <chr>, type <chr>, horizontalError <dbl>, depthError <dbl>,
## #   magError <dbl>, magNst <dbl>, status <chr>, locationSource <chr>,
## #   magSource <chr>
#### *Add the earthquake point data to the leaflet widget*
#### *Customize icons and set popups to show magnitude strength*
#### *View the western states out of interest* (Figure 3).

leaflet() %>% 
   setView(lng=-118.2437, lat = 34.0522, zoom = 5) %>%
  addProviderTiles(providers$Esri.WorldImagery) %>%
  addCircleMarkers(data = quakes_greater4.5, 
                   radius = 4, color = "red")

Figure 3. Default set-view centered over California’s west coast showing several earthquakes greater than 4.5 magnitude that occurred in northern California, along the Alaskan pan handle as well as along the west coast of Baja Mexico and northern South America in the past 30 days. Each red circle represents a single recorded earthquake in the past 30 days.

Exploratory data analysis

What was the depth range in kilometers of the quakes greater than 4.5 magnitude in the past 30 days?

#### *Use group_by() function to explore earthquake depth(km)*

quakes_greater4.5 %>% 
  group_by(depth) %>%
  count() %>%
  arrange(desc(n))
## # A tibble: 314 × 2
## # Groups:   depth [314]
##    depth     n
##    <dbl> <int>
##  1 10      274
##  2 35       15
##  3  2.87     1
##  4  3.83     1
##  5  5.54     1
##  6  5.66     1
##  7  5.85     1
##  8  6.06     1
##  9  6.96     1
## 10  7.14     1
## # ℹ 304 more rows

The majority of the quakes in the past 30 days greater than 4.5 magnitude were over 10.0km in depth.

#### * Filter the earthquake data by depth (greater than 10km) to gain insight*

quakes_greater4.5 %>%
  filter(depth >= 10)
## # A tibble: 581 × 22
##    time                latitude longitude depth   mag magType   nst   gap  dmin
##    <dttm>                 <dbl>     <dbl> <dbl> <dbl> <chr>   <dbl> <dbl> <dbl>
##  1 2023-06-09 22:46:20    16.5      146.  145.    4.5 mb         51    80  1.21
##  2 2023-06-09 22:25:00    -2.81     129.   10     4.7 mb         43    43  1.83
##  3 2023-06-09 21:33:05   -16.7     -174.   92.8   4.8 mb         49    49  4.08
##  4 2023-06-09 21:21:42   -60.5      160.   10     5.9 mww        63    56  6.06
##  5 2023-06-09 21:04:10   -23.3      171.   10     4.8 mb         28    52  3.02
##  6 2023-06-09 20:43:28   -47.3      -12.7  10     5.5 mb        111    60 16.5 
##  7 2023-06-09 11:33:35     7.20     -34.0  10     5.3 mww        65    55 12.8 
##  8 2023-06-09 10:47:57     2.71     128.   96.6   4.7 mb         38    79  2.14
##  9 2023-06-09 10:34:05    -2.81     129.   10     4.9 mb         61    63  1.84
## 10 2023-06-09 09:24:38    44.2      149.   53.0   4.6 mb         83   135  4.92
## # ℹ 571 more rows
## # ℹ 13 more variables: rms <dbl>, net <chr>, id <chr>, updated <dttm>,
## #   place <chr>, type <chr>, horizontalError <dbl>, depthError <dbl>,
## #   magError <dbl>, magNst <dbl>, status <chr>, locationSource <chr>,
## #   magSource <chr>

The tibble above tells us there are 581 earthquakes greater than 4.5 magnitude/greater than 10.0km depth in the data set.

Add labels to inform the users as to the magnitude of the world earthquakes (Figure 4).

#### *Piping and the ~ operator to  enable the popups to show world earthquakes
#### greater than 4.5 in the past 30 days using the tilde (~) operator to allow
#### quick reference from the pipe data*
#### *Build a better map with customized labels enabled by zooming and hovering using paste0() function and assess the magnitude of the quakes along the west coast of North America*

quakes_greater4.5 %>%
  leaflet() %>%
  addProviderTiles("Esri.WorldImagery") %>%
  addCircleMarkers(label = ~place, radius = 2, color = "red")

Figure 4. World earthquakes greater than 4.5 magnitude in the past 30 days with the most activity along the ring of fire along the east coast of Southeast Asia as well as along the west coast of North America. Labels enabled when hovering over earthquake point.

Finally, we will color code by magnitude to add both style and information about the earthquakes greater than 4.5 magnitude and further insight into distribution of stronger quakes.

  #### *Pass these colors to our earthquake point data (circles)*
#### *Label with magnitude strength when hovering over points*

leaflet() %>% 
   setView(lng=-118.2437, lat = 34.0522, zoom = 5) %>%
  addProviderTiles(providers$Esri.WorldImagery) %>%
  
    addCircleMarkers(data = quakes_greater4.5, label = ~place, radius = 3, color = "red") 

Figure 5. Earthquakes in the past 30 days greater than 4.5 magnitude along the west coast of California with quake places enabled by hovering over point.

The three earthquakes of interest highlighted in Figure 5 are listed below.

#### Add a Legend to enable reader to recall what the colored values mean
#### Color the quakes by magnitude strength

quakesgreater6.0 <- quakes_greater4.5 %>%
  filter(mag >= 6.0) # create data frame with quakes than 6.0 magnitude

#### Call the colorNumeric() function to create a new palette function

pal <- colorNumeric(
  palette = "Reds",
  domain = quakesgreater6.0$depth)
#### Add a Legend to enable reader to recall what the colored values mean
#### Color the quakes by magnitude strength

map1 <- leaflet(data = quakes_greater4.5) %>% # call leaflet function and add data
  addProviderTiles(providers$Esri.WorldImagery) %>% # add tiles
  addCircleMarkers(color = ~pal(depth),
                   radius = 2,
                   fillOpacity = .5,
                   stroke = FALSE,
                   popup = paste0("mag"),
                   label = ~place) # add earthquake points

title = "World Earthquakes greater than 4.5 magnitude in past 30 days"

map1

Figure 6. World earthquakes colored by depth(km) where red is deeper and grey is shallower. The leaflet widget has been enabled for accessing the place names while hovering.

Conclusions

Future Work

  • Analyze earthquake depth(km) using the complete World earthquakes greater than 1.0 magnitude database available and see if there are associations with many other variables.

  • Plot major fault lines relative to earthquakes and associate with magnitude and quake depth(km)

  • Plot unstable land cover that could cause creating a mudslide hazard and compare to quakes greater than 1.0 magnitude over the past 30 days.