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.

Package functions

Following are exported functions available to users:

This document serves as an introduction to the package and demonstrates how to use each of the individual features.

Clean the Data

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()

Timeline Visualization

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")

Visualize earthquakes on map

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       


gravatarsmiley/capstoneR documentation built on May 30, 2019, 4:06 a.m.