knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = TRUE
)
library(NOAAearthquake)
library(readr)
library(magrittr)
library(dplyr)
library(ggplot2)
library(leaflet)
library(lubridate)

NOAA Earthquake Analysis

This package has been developed as part of the capstone module for Coursera's Mastering Software Development in R Specialization. This package allows you to load earthquake data from the US National Centers for Environmental Information. These data contains information on destructive earthquakes from 2150 B.C. to the present that meet at least one of the following criteria:

How to load and clean the data

The function eq_clean_data() loads the .tsv data file downloaded from the search function on the NCEI website. It then creates a single Date field from the original Year, Mo and Dy fields, adding 01-01 where no month or day were present in the original data.

df1 <- eq_clean_data(system.file("extdata", "earthquakes.tsv", package = "NOAAearthquake"))
head(df1$Date)

The function eq_location_clean() then splits the original Location Name field at the : to generate a Location Name without the country, and a separate Country. Both fields are reformatted from all caps to capialisation as proper nouns.

df2 <- eq_location_clean(df1)
head(df2$Country)

How to generate a timeline plot

The function geom_timeline() generates a generates a basic ggplot2 visualisation of the timeline of earthquakes for a given year range and country. The size of the marker is proportional to the magnitude of the earthquake and the colour of the marker is scaled according to the the total number of deaths caused by the earthquake. The magrittr package must be loaded to make use of the %>% operator.

df2 %>% 
   dplyr::filter(Country %in% c("Mexico", "Turkey"), lubridate::year(Date) > 2000) %>% 
   ggplot2::ggplot(aes(x = Date, y = Country, color = `Total Deaths`, size = Mag)) +
   geom_timeline() +
   ggplot2::labs(size = "Richter scale value", col = "# Deaths")

To apply a cleaner aesthetic to the original plot, add the function theme_timeline().

df2 %>% 
   dplyr::filter(Country %in% c("Mexico", "Turkey"), lubridate::year(Date) > 2000) %>% 
   ggplot2::ggplot(aes(x = Date, y = Country, color = `Total Deaths`, size = Mag)) +
   geom_timeline() +
   theme_timeline() +
   ggplot2::labs(size = "Richter scale value", col = "# Deaths")

Finally, the function geom_timeline_label() applies Location Name labels to the earthquake markers. This label is only applied to the highest magnitude for that year range, the number being labelled being specified by the n_max argument.

df2 %>% 
   dplyr::filter(Country %in% c("Mexico", "Turkey"), lubridate::year(Date) > 2000) %>% 
   ggplot2::ggplot(aes(x = Date, y = Country, color = `Total Deaths`, size = Mag)) +
   geom_timeline() +
   geom_timeline_label(aes(label = `Location Name`), n_max = 3) +
   theme_timeline() +
   ggplot2::labs(size = "Richter scale value", col = "# Deaths")

How to generate an interactive map

The function eq_map() generates a Leaflet map plotting earthquakes for a specified country and year range, with a basic marker popup date label formatted as YYYY-MM-DD.

df2 %>% 
   dplyr::filter(Country == "Turkey", lubridate::year(Date) >= 2010) %>%  
   eq_map(annot_col = "Date")

Adding the function eq_create_label() generates more detailed popup labels with Location, Magintude and Total Deaths fields. Where data for these fields do not exist in the data (particularly for historical earthquakes), the respective field will not display on the label.

df2 %>% 
   dplyr::filter(Country == "Turkey", lubridate::year(Date) >= 2010) %>%  
   dplyr::mutate(popup_text = eq_create_label(.)) %>%
   eq_map(annot_col = "popup_text")


csgbloom/NOAAearthquake documentation built on April 12, 2021, 12:40 p.m.