This R package contains a set of tools for cleaning and visualizing earthquake data from the NOAA.

Installation

You need to use devtools to install this package:

install.packages("devtools")
devtools::install_github("jdallmann/NOAAviz")

Retrieving Data

The data for use with this package can be downloaded at https://www.ngdc.noaa.gov/. Significant earthquakes can be downloaded as follows:

\dontrun{
paste0("https://www.ngdc.noaa.gov/",
        "nndc/struts/results?type_0=Exact&",
        "signif.txtquery_0=$ID&t=101650&s=13&",
        "d=189&dfn=signif.txt") %>%
    readr::read_delim(delim = "\t")
}

Examples for functions

eq_datemaker

The eq_datemaker helper function takes in YEAR, MONTH, and DAY number from NOOA data controls for BCE dates and missing values, and returns a date in posixCt format.

\dontrun{
    eq_datemaker(-1142, 11, NA)
}

eq_location_clean

Given a LOCATION from NOAA data strips out the country name and ':', leaving all characters following and converting to title case. Non-exported helper function.

\dontrun{
    eq_location_clean("Canada: Land of the free")
}

eq_clean_data

This function takes a NOAA data frame, and cleans it for analysis.

\dontrun{
paste0("https://www.ngdc.noaa.gov/",
       "nndc/struts/results?type_0=Exact&",
       "signif.txtquery_0=$ID&t=101650&s=13&",
       "d=189&dfn=signif.txt") %>%
    readr::read_delim(delim = "\t") %>%
    eq_clean_data()
}

geom_timeline

geom_timeline is used for "plotting a time line of earthquakes ranging from xmin to xmaxdates with a point for each earthquake. Optional aesthetics include color, size, and alpha (for transparency). The xaesthetic is a date and an optional y aesthetic is a factor indicating some stratification in which case multiple time lines will be plotted for each level of the factor (e.g. country)." (Mastering Software Development in R Capstone, Week 2)

\dontrun{
paste0("https://www.ngdc.noaa.gov/",
       "nndc/struts/results?type_0=Exact&",
       "signif.txtquery_0=$ID&t=101650&s=13&",
       "d=189&dfn=signif.txt") %>%
    readr::read_delim(delim = "\t") %>%
    eq_clean_data() %>%
    dplyr::filter(lubridate::year(DATE) >= 2000,
                  COUNTRY == "MEXICO") %>%
    ggplot2::ggplot(aes(x = DATE)) +
    geom_timeline()
}

geom_timeline_label

geom_timeline_label is used for adding annotations to NOAA earthquake data. "This geom adds a vertical line to each data point with a text annotation (e.g. the location of the earthquake) attached to each line. There should be an option to subset to n_max number of earthquakes, where we take the n_max largest (by magnitude) earthquakes. Aesthetics are x, which is the date of the earthquake and label which takes the column name from which annotations will be obtained." (Mastering Software Development in R Capstone, Week 2)

\dontrun{
paste0("https://www.ngdc.noaa.gov/",
       "nndc/struts/results?type_0=Exact&",
       "signif.txtquery_0=$ID&t=101650&s=13&",
       "d=189&dfn=signif.txt") %>%
    readr::read_delim(delim = "\t") %>%
    eq_clean_data() %>%
    dplyr::filter(COUNTRY = "ANTARTICA") %>%
    ggplot2::ggplot(aes(x = DATE, Y = COUNTRY)) +
    geom_timeline() +
    geom_timeline_label(aes(label = LOCATION_NAME),
                        n_max = 5)
}

eq_map

eq_map maps the epicenters (LATITUDE/LONGITUDE) with a radius proportional to the earthquake's magnitude (EQ_PRIMARY) "and annotates each point with in pop up window containing annotation data stored in a column of the data frame. The user should be able to choose which column is used for the annotation in the pop-up with a function argument named annot_col." (Mastering Software Development in R Capstone, Week 3)

\dontrun{
    clean_NOAA_data %>%
         dplyr::filter(COUNTRY == "JAPAN" &
                       lubridate::year(DATE) >= 2000) %>%
         eq_map(annot_col = "DATE")
}

eq_create_label

eq_create_label takes a row of the dataset as an argument and creates an HTML label that can be used as the annotation text in the leaflet map." It puts together "a character string for each earthquake that will show the cleaned location, the magnitude (EQ_PRIMARY), and the total number of deaths (TOTAL_DEATHS), with boldface labels for each ("Location", "Total deaths", and "Magnitude"). If an earthquake is missing values for any of these, both the label and the value is skipped for that element of the tag." (Mastering Software Development in R Capstone, Week 3)

\dontrun{
    clean_NOAA_data %>%
         dplyr::filter(COUNTRY == "JAPAN" &
                       lubridate::year(DATE) >= 2000) %>%
         dplyr::mutate(popup_text = eq_create_label(.)) %>%
         eq_map(annot_col = "popup_text")
}


jdallmann/NOAAviz documentation built on Nov. 4, 2019, 2:35 p.m.