knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" ) library(knitcitations) library(tidyverse) cite_options(citation_format = "pandoc")
(tie-dye graphs)
Long time series datasets can be difficult to plot and examine using static
figures. Interactive plots using the
dygraphs package are a great tool but are
currently not easily implemented for tidy data. This package provides methods
for dygraph()
to make it easier to plot tidy time series. Currently there are
methods for dataframes and tibbles, grouped tibbles, and tsibbles.
Currently tydygraphs is only available on GitHub. You can install the current development version using remotes or by downloading and building from source:
# Install using remotes remotes::install_github("jpshanno/tydygraphs") # or from source tydygraphs_source <- file.path(tempdir(), "tydygraphs-master.zip") download.file("https://github.com/jpshanno/tydygraphs/archive/master.zip", tydygraphs_source) unzip(tydygraphs_source, exdir = dirname(tydygraphs_source)) install.packages(sub(".zip$", "", tydygraphs_source), repos = NULL, type = "source")
The dataset great_lakes_hydro
included in tydygraphs demonstrates the problems
with examing long-term time series datasets and the benefits of using dygraphs.
The dataset consists of monthly water levels, precipitation, evaporation,
discharge from, and runoff into Lake Superior and the combined Lakes Michigan
and Huron. The data were downloaded from NOAA's Great Lakes Dashboard
Project r citep("10.1016/j.envsoft.2015.12.005")
library(tydygraphs)
great_lakes_hydro
Let's start simple and show how the we can use the function for a single time
series by filtering and supplying just a single variable to dygraph
. The first
column representing a time series will be used if nothing is supplied for time
.
great_lakes_hydro %>% filter(lake == "Superior") %>% dygraph(water_level_m)
And if you noticed in the data, all of the other variables are expressed in millimeters, but because tydygraphs is built on top of dygraphs we can easily add two a second y-axis to look at precipitation in the same plot. This plot also makes it pretty clear why a tool like dygraphs is great for long time series. When the entire dataset is displayed it is nearly useless for exploratory analysis or diagnostics, but now we have the ability to zoom in on periods of interest.
two_variables <- great_lakes_hydro %>% filter(lake == "Superior") %>% dygraph(precip_mm, water_level_m) %>% dySeries("precip_mm", axis = 'y2') two_variables
If we wanted to use this plot in something like a Shiny application or a blog post we could build even more using the dygraphs customizations. The chunk below makes precipitation into a step plot, adds a border around the water level series, and starts the plot zoomed in on a period of interest.
There are lots of ways to customize dygraphs and I highly recommend looking at the documentation for dygraphs.
# Start with the plot from above two_variables %>% # Modify precipitation to make a filled step plot with no border by adjusting # the strokePattern dySeries("precip_mm", axis = "y2", stepPlot = TRUE, fillGraph = TRUE, color = "dodgerblue", strokePattern = c(0,1)) %>% # Modify water level to add a white border around the line to ensure it stands # out from precipitation dySeries("water_level_m", strokeBorderWidth = 2, color = "darkblue") %>% dyRangeSelector(dateWindow = c("2000-01-01", "2010-01-01")) %>% dyLegend(labelsSeparateLines = TRUE)
And the final example is showing how we can plot our tidy data by passing a
grouped tibble to dygraph()
.
great_lakes_hydro %>% group_by(lake) %>% dygraph(water_level_m)
write.bibtex(file="references.bib") RefManageR::PrintBibliography(read.bibtex(file = "references.bib"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.