library(CourseraRcapstone)
library(magrittr)
library(ggplot2)
library(grid)
options(width = 120)

Read and clean the data

Read the raw data manually then pass to the eq_clean_data function

raw_data_fn <- file.path(system.file("extdata", package="CourseraRcapstone"),"signif.txt")
data <- readr::read_delim(raw_data_fn, "\t") %>% eq_clean_data
str(data)

Or use directy the built in read_eq_clean_data function

data2 <- read_eq_clean_data(raw_data_fn)
identical(data, data2)

Or, in more details

Draw earthquake timeline

The follong snippet draws earthquakes observed in the US and Canada from 2000

    read_eq_clean_data() %>%
        dplyr::filter(YEAR > 2000 & !IS_BC & COUNTRY %in% c("USA", "CANADA")) %>%
        dplyr::mutate(TOTAL_DEATHS = as.numeric(TOTAL_DEATHS),
                      EQ_PRIMARY = as.numeric(EQ_PRIMARY)) %>%
        ggplot2::ggplot(ggplot2::aes(x = DATE,
                                     y = COUNTRY,
                                     colour = TOTAL_DEATHS,
                                     size = EQ_PRIMARY
                                     )) +
        geom_timeline() +
    theme_timeline() +
    labs(size = "Richter scale value", color = "# deaths", fill = "# deaths")

Draw earthquake timeline with labels

The follong snippet draws earthquakes observed in the US and Canada from 2000 using: geom_timeline as previously to draw the timeline geom_timeline_label to add the labels.`

    read_eq_clean_data() %>%
        dplyr::filter(YEAR > 2000 & !IS_BC & COUNTRY %in% c("USA", "CANADA")) %>%
        dplyr::mutate(TOTAL_DEATHS = as.numeric(TOTAL_DEATHS),
                      EQ_PRIMARY = as.numeric(EQ_PRIMARY)) %>%
        ggplot2::ggplot(ggplot2::aes(x = DATE,
                                     y = COUNTRY,
                                     colour = TOTAL_DEATHS,
                                     size = EQ_PRIMARY
                                     )) +
        geom_timeline() +
    theme_timeline() +
    labs(size = "Richter scale value", color = "# deaths") +
    geom_timeline_label(ggplot2::aes(label = LOCATION_NAME), n_max = 3)

Visualize the earthquakes on a map with leaflet

The follong snippet map the earthquakes observed in the Mexico from 2000. It uses the input that we saw previously the eq_map function that draw a circle at each earthquake * the eq_create_label function that create labels.

    read_eq_clean_data() %>%
        dplyr::filter(YEAR > 2000 & !IS_BC & COUNTRY %in% c("MEXICO")) %>%
        dplyr::mutate(popup_text = eq_create_label(.)) %>%
        eq_map(annot_col = "popup_text")


learner42/CourseraRcapstone documentation built on May 30, 2019, 4:37 p.m.