This package processes and visualizes NOAA earthquake data.
The package cleans and tidy's the data, creates static timeline plots, and creates dynamic leaflet plots.
Before we begin, let's load in an R dataset:
library(earthquake) library(dplyr) library(ggplot2) library(grid) data(earthquake_raw)
earthquake_raw is a data.frame that holds the raw NOAA data.
The NOAA datasets do not come by default in a clean format. The earthquake package includes tools for cleaning data.
One of the tools is the eq_clean_data
function:
earthquake_clean <- earthquake_raw %>% earthquake::eq_clean_data() head(earthquake_clean[["DATE"]])
Many common R date functions do not support BCE dates. Most earthquake analysis focuses on more recent events. Therefore we drop BCE dates to maintain user flexibility.
The eq_clean_data
creates a date column, removes NA columns and drops BCE dates.
Another tool is the eq_location_clean
function:
earthquake_clean_loc <- earthquake_raw %>% earthquake::eq_location_clean() #Original Location Name head(earthquake_raw[["LOCATION_NAME"]]) #Cleaned Location Name head(earthquake_clean_loc[["LOCATION_NAME"]])
The eq_location_clean
cleans up the location name for future annotations.
The earthquake package also includes new geoms for plotting timelines.
The geom_timeline
creates timeline plots of the cleaned NOAA data:
ggplot(data=earthquake_clean %>% filter(COUNTRY %in% c("CHINA", "USA") ))+ geom_timeline(aes(x = DATE, color = log(DEATHS, base = 10), y = COUNTRY, alpha = 1, xmin=as.Date("2000-01-01"), xmax=as.Date("2015-12-31"), size = EQ_PRIMARY)) + scale_color_continuous()+ theme_minimal()+theme(legend.position = "bottom")
You can add in the geom_timelinelabel
geom to make annotations. It has an additional parameter n_max.
This only labels the highest n_max magnitude earthquakes.
ggplot(data=earthquake_clean %>% filter(COUNTRY %in% c("CHINA", "USA") ))+ geom_timelinelabel(aes(x = DATE, label= LOCATION_NAME, y = COUNTRY, xmin=as.Date("2000-01-01"), xmax=as.Date("2015-12-31"), mag = EQ_PRIMARY), n_max = 4)+ geom_timeline(aes(x = DATE, color = log(DEATHS, base = 10), y = COUNTRY, alpha = 1, xmin=as.Date("2000-01-01"), xmax=as.Date("2015-12-31"), size = EQ_PRIMARY)) + scale_color_continuous()+ theme_minimal()+theme(legend.position = "bottom")
The eq_map
function takes in a cleaned NOAA earthquake dataset, and an annotation column.
It outputs a leaflet plot with the earthquakes on a map and annotations (available by clicking on the point) that describe them.
earthquake_clean %>% dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>% eq_map(annot_col = "DATE")
The eq_create_label
function assumes that the dataset has columns titled LOCATION_NAME, EQ_PRIMARY, and deaths that describe:
the name of the location of the earthquake, the magnitude of the earthquake, and the number of deaths associated with the earthquake, respectively.
It then creates an html tagged output for use in the eq_map
annotations.
earthquake_clean %>% dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>% dplyr::mutate(popup_text = eq_create_label(.)) %>% eq_map(annot_col = "popup_text")
The earthquake
package contains functions for cleaning NOAA earthquake data, plotting timelines, and making leaflet plots.
I hope you enjoy!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.