1.) Connect to the API with format as CSV

Because this API did not have an R package, the query had to be set up manually. This was done with the httr package; using the protocol of the USGS Earthquake Hazards Program, a map can be created that illustrates all of the earthquakes in the past year that were able to be felt, meaning the magnitude was above 3. The code below illustrates how the data was collected:

#libraries
library(httr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#set request parameters
format = "csv"
starttime = "2022-01-01"
endtime = "2022-09-05"
minmagnitude = "3"
orderby = "magnitude"

#Create request URL and execute
Request_url = paste("https://earthquake.usgs.gov/fdsnws/event/1/query?format=",
                    format,
                    "&starttime=",
                    starttime,
                    "&endtime=",
                    endtime,
                    "&minmagnitude=",
                    minmagnitude,
                    "&orderby=",
                    orderby)
EQget = GET(Request_url)

# Parse and convert to dataframe
df = read.table(text = content(EQget,'text'),
                sep = ",",
                header=TRUE,
                stringsAsFactors = FALSE)

# Show sample of the data once extracted
#Random Sample of 5 rows and first 5 columns
data_sample = df[sample(1:nrow(df),5),1:5]
knitr::kable(data_sample, format="markdown")
time latitude longitude depth mag
5657 2022-03-21T15:20:27.807Z 37.5030 72.2188 183.49 4.4
9689 2022-06-14T03:54:45.818Z -10.4015 110.3767 10.00 4.1
12507 2022-06-01T16:48:14.279Z 53.6856 -164.8534 21.55 3.1
3584 2022-07-08T17:15:49.560Z 18.8162 147.1272 10.00 4.6
2302 2022-03-17T09:17:24.955Z 25.1301 -45.5399 10.00 4.7

2.) Using this data, plot onto an interactive map

Once the data has been collected, it can then be used to develop a map. The map created below will highlight all of the earthquakes epicenters based on their longitude and latitude. There are also labels for each point which once clicked on will indicate the place, type, magnitude, and time.

#libraries
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.0.5
#Create the Map
map = leaflet(df) %>% 
  addTiles() %>%
  addCircles(lng = ~longitude, 
             lat = ~latitude, 
             radius = 200, 
             label = ~place, 
             popup = ~paste(place,
                            "<br>Type:",
                            type,
                            "<br>Magnitude:",
                            mag, 
                            "<br>Time:",
                            time))

#print the map
map