library(r.prog.capstone)
library(dplyr)
library(ggplot2)
library(readr)
library(grid)
library(lubridate)
library(leaflet)
library(scales)

This vignette gives a brief overview of the r.prog.capstone 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.

Installation

To install the package type the following (the development version from Github):

library(devtools)
install_github("Solymi90/r.prog.capstone")
library(r.prog.capstone)

Package functions

There are six exported functions available to users:

Please find a short description with examples on how to use the functions. The example data from NOAA can be found in the package directory under \extdata folder.

Clean data

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.

file_name <- system.file("extdata/data.gz", package = "r.prog.capstone")
data <- readr::read_delim(file_name, delim = "\t")

eq_clean_data(data)

Visualize earthquake timeline

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

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:

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


DanielaSempertegui/Mastering-Software-Development-en-R-Capstone documentation built on May 16, 2019, 8:10 p.m.