knitr::opts_chunk$set( collapse = TRUE, comment = "#>", echo = TRUE ) library(NOAAearthquake) library(readr) library(magrittr) library(dplyr) library(ggplot2) library(leaflet) library(lubridate)
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:
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)
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")
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.