knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(lmstt)
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.
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"))) ))
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"))
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()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.