library(ggplot2) library(magrittr) library(leaflet) library(lubridate) library(earthquakecapstone) knitr::opts_chunk$set( echo = TRUE, fig.align = "center", fig_width = 8, fig_height = 5 )
This package is the capstone project for Coursera's Mastering Software Development in R specialization consisting of the courses
The package provides functions to clean and visualize data from the NOAA Significant Earthquake Database.
Install the development version of this package using
devtools::install_github("lemonad/coursera-mastering-r-capstone")
The NOAA Significant Earthquake database can be made available by executing the following
api_uri <- "https://www.ngdc.noaa.gov/hazel/hazard-service/api/v1/earthquakes" f <- jsonlite::read_json(api_uri, simplify = TRUE) earthquakes <- dplyr::as_tibble(f$items)
The API data has some quirks and can be made easier to work with by
using the function eq_clean_data
which returns a dataframe that can be
subsequently filtered, etc.
df <- earthquakes %>% eq_clean_data() %>% dplyr::filter( country %in% c("USA", "Mexico") & lubridate::year(date) >= 2000 )
Note that the BCE events are removed from the results per the requirements
of using the class Date
for the dates. This could be remedied by using the
package gregorian.
Having cleaned the data, the events can now be plotted on a timeline with optional label annotation. Below, the timeline is plotted with a separate axis for each country and events are colored according to the total number of deaths. The five highest magnitude events are annotated with their corresponding location.
df %>% ggplot2::ggplot( ggplot2::aes(x = date, y = country, colour = deathsTotal), alpha = ) + geom_timeline() + geom_timeline_label( ggplot2::aes(label = locationName, magnitude = eqMagnitude), n_max = 5 )
The sites of the events can also be plotted on an interactive map. The sites
will show a tooltip containing details when clicked. Here, the helper
function eq_create_label
is used to create the underlying html for the
tooltip.
df %>% 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.