knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(lmstt)

Data

The data came from the Bureau of Transportation Statistics. Only the pollution incidents where the Coast Guard investigated as the lead agency are included in this dataset. Data on spills where the Environmental Protection Agency or any of the state authorities are the lead agency is not included. These are mostly offshore spills as the EPA usually handles inshore spills. These statistics cover yearly gallons spilled, number of incidents, and source type from 1985, 1990, 1995-2020.

Vessel, Non-Vessel, and Mystery Sources

Gallons Spilled

oilSpills |> dplyr::filter(Source == "Vessel sources, total" | 
                     Source == "Nonvessel sources, total"|
                     Source == "Mysteryc") |> 
  plotly::plot_ly(x = ~Year, y =~`Gallons spilled`, color = ~Source, type="bar", colors = 
            c("#a6cee3", "#1f78b4", "#b2df8a"),
           width = 1000) |>
  plotly::layout(yaxis = list(fixedrange = FALSE),
         updatemenus = list(
            list(
              type = "buttons",
              x = 0.6,
              y = 1,
              direction = "right",
              buttons = list(

                list(method = "relayout",
                args = list("barmode","dodge"),
                label = "Grouped"),

                list(method = "relayout",
                args = list("barmode","stack"),
                label = "Stacked")))


  ))

"On April 20, 2010, the oil drilling rig Deepwater Horizon, operating in the Macondo Prospect in the Gulf of Mexico, exploded and sank resulting in the death of 11 workers on the Deepwater Horizon and the largest spill of oil in the history of marine oil drilling operations."

Incidents

oilSpills |> dplyr::filter(Source == "Vessel sources, total"| 
                     Source == "Nonvessel sources, total"|
                     Source == "Mysteryc")  |>
  tidyr::drop_na() |>
  plotly::plot_ly(width = 1000) |>
  plotly::add_trace(x = ~Year, y = ~Incidents, type = 'scatter', color =~Source, mode = 'lines+markers', 
            colors = c("#a6cee3", "#1f78b4", "#b2df8a"))

Source, Incidents, and Gallons Spilled

y2 <- list(
  overlaying = "y",
  side = "right",
  title = "Number of Incidents",
  automargin = T)

oilSpills |> dplyr::filter(Source != "TOTAL all spills", 
                     Source != "Vessel sources, total", 
                     Source != "Nonvessel sources, total")  |>
  tidyr::drop_na() |>
  dplyr::mutate(Source = forcats::fct_reorder(Source, dplyr::desc(`Gallons spilled`))) |>
  plotly::plot_ly(x = ~Year, y =~`Gallons spilled`, color = ~Source, type="bar", colors = "Accent",
          width = 1000, height = 500) |>
  dplyr::group_by(Year) |>
  dplyr::summarise(n = sum(Incidents)) |>
  plotly::add_trace(x = ~Year, y = ~n, type = 'scatter',  mode = 'markers', 
            name = "Incidents", yaxis = "y2", color = I("black")) |>
  plotly::layout(yaxis2 = y2,
         yaxis = list(fixedrange = FALSE),
          updatemenus = list(
            list(
              type = "buttons",
              x = 0.6,
              y = 1,
              direction = "right",
              buttons = list(

                list(method = "relayout",
                args = list("barmode","dodge"),
                label = "Grouped"),

                list(method = "relayout",
                args = list("barmode","stack"),
                label = "Stacked")))
  )) |>
  plotly::rangeslider()

References



lmsteffen/DS303_Rpackage documentation built on April 14, 2022, 11:45 a.m.