The eqvis
package provides functions for cleaning and plotting the NOAA significant earthquake dataset using either a timeline or map. A sample of the NOAA data sample_eq
is provided.
The earthquake data must be cleaned to utilize the timeline and map functions by using the eq_clean_data
function. With this function, the country and colon are removed from the LOCATION_NAME
and it is converted to title case. This provides for a better label. A DATE
column is created from the date parts (YEAR
, MONTH
, DAY
). The LATITUDE
and LONGITUDE
columns are converted to numerics.
This example demonstrates the use of eq_clean_data
showing the before and after values. Notice the difference in values for the location name and the added DATE
.
library(eqvis) sample3 <- head(sample_eq[,c("YEAR","MONTH","DAY","LOCATION_NAME","LATITUDE","LONGITUDE")],3) print.data.frame(sample3,row.names=FALSE) print.data.frame(eq_clean_data(sample3),row.names=FALSE)
A timeline can be produced using ggplot2
with the geom_timeline
/stat_timeline
functions. It will plot a timeline with points for each earthquake and can optionally include a size and color/fill aesthectics. If xmin
and xmax
aesthetics are provided, stat_timeline
will subset the data. The geom_timeline_label
function can be used to add a label geom. It wil print the label aesthetic for the top n_max
earthquakes in terms of magnitude.
This example demonstrates the use of eq_clean_data
, geom_timeline
, and geom_timeline_label
.
library(ggplot2) library(scales) library(eqvis) ggplot(data=subset(eq_clean_data(sample_eq), !is.na(EQ_PRIMARY) & COUNTRY %in% c("USA")), aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY, xmin = as.Date('2012-01-01',"%Y-%m-%d"), xmax = as.Date('2016-12-31',"%Y-%m-%d"), color = DEATHS, fill = DEATHS, label = LOCATION_NAME)) + geom_timeline(alpha = .5) + scale_size_continuous(name = "Richter Scale Value") + scale_color_continuous(name = "# Deaths") + scale_fill_continuous(guide=FALSE) + labs(x = "DATE", y = "") + geom_timeline_label(aes(n_max = 3)) + theme(legend.position = "bottom", legend.key = element_blank(), legend.box = "horizontal", panel.background = element_blank(), axis.line.x = element_line(size = 1, color = "black"), axis.ticks.x = element_line(size = 1))
A map can be produced using eq_map(). It will display circle markers at the lat/lon locations with a radius based on the magnitude of the earthquake and popup text reflecting the annotation column provided. An optional eq_create_label function is provided that will generate label text popup_text
using the location name, magnitude and deaths for the earthquake.
This example demonstrates the use of eq_clean_data
, eq_create_label
, and eq_map
.
library(eqvis) library(leaflet) library(magrittr) library(dplyr) library(lubridate) sample_eq %>% eq_clean_data() %>% dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>% dplyr::mutate(popup_text = eq_create_label(.)) %>% eq_map(annot_col = "popup_text")
A sample_eq
dataset is provided. It is a subset of the complete NOAA dataset, filtered for COUNTRY
values USA
and MEXICO
and YEAR
2000-2017. It has the following structure:
str(sample_eq)
The complete NOAA earthquake dataset can be accessed from: https://www.ngdc.noaa.gov/nndc/struts/results?type_0=Exact&query_0=$ID&t=101650&s=13&d=189&dfn=signif.txt. It contains information about 5,933 earthquakes around the world over an approximately 4,000 year time span.
Note: This package was developed to work with positive years (A.D.) only.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.