tests/testthat/test_eq_geoms.R

library(earthquake)

# Load, clean and filter data.

data("earthquakes")

earthquakes <- earthquakes %>%
      eq_clean_data() %>%
      eq_location_clean() %>%
      eq_select_data() %>%
      eq_filter_data(countries = c("Indonesia", "Japan", "Russia"),
                     minimum_date = "2000-01-01",
                     maximum_date = "2018-12-31")

context("geom_timeline")

test_that("Timeline plot", {

      g <- ggplot2::ggplot(data = earthquakes, ggplot2::aes(x = DATE, y = COUNTRY)) +
            geom_timeline(xmin = "2000-01-01", xmax = "2018-12-31",
                          ggplot2::aes(color = TOTAL_DEATHS, size = EQ_PRIMARY)) +
            ggplot2::labs(title = "NOAA Significant Earthquakes",
                          subtitle = "Plot of events for Indonesia, Japan and Russia on individual timelines.",
                          x = "Timeline",
                          y = "",
                          color = "Total Deaths",
                          size = "Magnitude")

      plot_data <- g$data

      # Source data is used correctly.

      expect_equal(plot_data, earthquakes)

      # Plot has a two layers.

      expect_equal(length(g$layers), 1)

})

context("geom_timeline_label")

test_that("Timeline labels", {

      g <- ggplot2::ggplot(data = earthquakes, ggplot2::aes(x = DATE)) +
            geom_timeline(xmin = "2000-01-01", xmax = "2018-12-31",
                          ggplot2::aes(color = TOTAL_DEATHS, size = EQ_PRIMARY)) +
            geom_timeline_label(n_max = 5,
                              ggplot2::aes(label = LOCATION_NAME, magnitude = EQ_PRIMARY)) +
            ggplot2::labs(title = "NOAA Significant Earthquakes",
                          subtitle = "Plot of events for Indonesia, Japan and Russia combined.",
                          x = "Timeline",
                          y = "",
                          color = "Total Deaths",
                          size = "Magnitude")

      plot_data <- g$data

      # Source data is used correctly.

      expect_equal(plot_data, earthquakes)

      # Plot has a second layer for labels.

      expect_equal(length(g$layers), 2)

})
dtminnick/earthquake documentation built on Nov. 4, 2019, 11:04 a.m.