library(earthquakeVis) library(dplyr) library(ggplot2) library(readr)
This vignette gives a brief overview of the earthquakeVis R package created for the purpose of visualizing NOAA earthquake data. It processes data from NOAA database and visualizes them using ggplot2
and leaflet
packages.
There are six exported functions available to users:
eq_clean_data()
geom_timeline()
geom_timeline_label()
theme_timeline()
eq_create_label()
eq_map()
Further we give a short description with examples how to use the functions. For the purposes of these examples we will use data from NOAA that can be found in the package directory under \extdata
folder.
The first function is required to clean data for the visualization. It creates a DATE column in Date
format, transforms latitude and longitude to numeric format and trims country from LOCATION_NAME.
filename <- system.file("extdata/earthquakes.tsv.gz", package = "earthquakeVis") data <- readr::read_delim(filename, delim = "\t") eq_clean_data(data)
The next three functions utilize ggplot2
package to visualize earthquake timeline. The basic geom_timeline()
geom requires clean data from the previous paragraph. The required aesthetics is x
with dates, optional are y
for grouping by country, and size
and color
that can be use according to user needs. The geom_timeline_label()
function requires additional label
aesthetic for labeling. For better visualization of these two geoms, theme_timeline()
theme was added. Here is an example:
data %>% eq_clean_data() %>% filter(COUNTRY %in% c("GREECE", "ITALY"), YEAR > 2000) %>% ggplot(aes(x = DATE, y = COUNTRY, color = as.numeric(TOTAL_DEATHS), size = as.numeric(EQ_PRIMARY) )) + geom_timeline() + geom_timeline_label(aes(label = LOCATION_NAME), n_max = 5) + theme_timeline() + labs(size = "Richter scale value", color = "# deaths")
The package utilized leaflet
functions to visualize earthquakes on a map using eq_map()
function. The map is automatically trimmed to display the input data frame. Optional annotations can be created using eq_create_label()
function. The result is an interactive map where user can click on individual points to get details:
data %>% eq_clean_data() %>% dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>% 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.