knitr::opts_chunk$set(collapse = TRUE, comment = "#>") options(tibble.print_min = 4L, tibble.print_max = 4L) library(capstoneR) library(ggplot2) library(dplyr) library(readr) library(leaflet) library(lubridate)
capstoneR is a package developed as the capstone project for the Advanced R Programming specialization on Coursera. This vignette gives a brief overview of the capstoneR 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.
Following are exported functions available to users:
eq_clean_data()
geom_timeline()
geom_timeline_label()
eq_create_label()
eq_map()
This document serves as an introduction to the package and demonstrates how to use each of the individual features.
Some cleaning is required to get the data ready to work nicely with the visualization functions provided below. The easist thing to do is simply to load the clean dataset
included with this package using the data()
function:
data('clean_NOAA')
clean_NOAA <- readr::read_delim('signif.txt',delim = '\t') %>% eq_clean_data()
A timeline is a great way to see when significant earthquakes have ocurred. This package includes a custom geom
and stat
to make creating just such a plot easier with the widely-used ggplot2
package. The basic geom_timeline()
geom requires clean data from the previous paragraph. The geom_timeline_label()
function requires additional label
aesthetic for labeling.
filter(clean_NOAA, COUNTRY == "USA") %>% ggplot(aes(x = DATE)) + geom_timeline(x_min = ymd("2000-01-01"), x_max = ymd("2017-07-01")) + theme_minimal() + theme(panel.grid.major.x = element_blank()) + theme(axis.text.y = element_blank()) + theme(panel.grid.minor.x = element_blank()) + scale_y_continuous(breaks = 1, limits = c(0.5, 2)) + labs(x = "Date")
filter(clean_NOAA, COUNTRY == "USA" | COUNTRY == "CANADA") %>% ggplot(aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY)) + geom_timeline(x_min = ymd("2000-01-01"), x_max = ymd("2017-07-01")) + theme_minimal() + theme(panel.grid.major.x = element_blank()) + theme(panel.grid.minor.x = element_blank()) + theme(legend.position = "bottom") + theme(legend.key.size = unit(0.078, 'npc')) + labs(x = "Date", y = "Country", size = "Magnatude")
xmin <- ymd("2000-01-01") xmax <- ymd("2017-07-01") filter(clean_NOAA, COUNTRY == "CANADA" | COUNTRY == "USA") %>% ggplot(aes(x = DATE, y = COUNTRY, size = EQ_PRIMARY)) + geom_timeline(x_min = xmin, x_max = xmax) + geom_timeline_label(aes(label = LOCATION_NAME, magnatude = EQ_PRIMARY), x_min = xmin, x_max = xmax, top_x_mag = 5) + theme_minimal() + theme(panel.grid.major.x = element_blank()) + theme(panel.grid.minor.x = element_blank()) + theme(legend.position = "bottom") + theme(legend.key.size = unit(0.078, 'npc')) + labs(x = "Date", y = "Country", size = "Magnatude")
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:
map <- clean_NOAA %>% dplyr::filter(COUNTRY == "MEXICO" & lubridate::year(DATE) >= 2000) %>% dplyr::mutate(popup_text = eq_create_label(.)) %>% eq_map(annot_col = "popup_text") map
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.