library(dplyr)
library(dygraphs)
Dynamic Time-Series Visualization with dygraphs in R: A Comprehensive Guide to Interactive and Customizable Charts.
Introduction:
The dygraphs package in R serves as a bridge to the dygraphs JavaScript charting library, enabling users to create visually appealing and interactive time-series charts. It has the following key features:
Automated Plotting: It effortlessly generates charts for time-series data, specifically designed to handle xts time series objects or any object that can be converted to xts.
Customizable Display: Users have extensive control over the appearance of the chart, allowing for highly configurable axes and series representation. It even supports the inclusion of a secondary Y-axis if needed.
Interactive Elements: The package provides interactive features such as zooming and panning, enhancing the user’s ability to explore and analyze time-series data effectively. Additionally, it supports highlighting specific series or points of interest.
Upper/Lower Bars: Users can incorporate upper and lower bars, useful for indicating prediction intervals or other types of confidence intervals around the plotted series.
Graph Overlays: The package supports various graph overlays, including shaded regions, event lines, and point annotations. These features add additional context and information to the time-series chart.
Here’s a simple dygraph created from a multiple time series object.Let’s check it.
Step 1: Installation
You can install the dygraphs package from CRAN as follows:
install.packages(“dygraphs”)
Note : You can use dygraphs at the R console, within R Markdown documents, and within Shiny applications.
Step 2 : Load the package using the following command.
head(df1)
Year Month Sales_Product1 Sales_Product2
1 2013 Jan 364 361
2 2013 Feb 234 273
3 2013 Mar 232 367
4 2013 Apr 280 478
5 2013 May 380 468
6 2013 Jun 233 432
<-ts(df1$Sales_Product1,start = c(2013,1),end = c(2015,12),frequency = 12)
ts1<-ts(df1$Sales_Product2,start = c(2013,1),end = c(2015,12),frequency = 12)
ts2
<-cbind(ts1,ts2)
ts_opdygraph(ts_op)
This graph is super easy to play with! When you move your mouse over the lines, you’ll see the exact values at each point. It’s like magic for exploring your data!
Now let’s explore its features.
- dyRangeSelector() : You can customize dygraphs by piping additional commands onto the original dygraph object. Here we pipe a dyRangeSelector onto our original graph
dygraph(ts_op) %>% dyRangeSelector()
- dySeries() and dyOptions() : dySeries() allows you to define multiple series to be displayed on the same chart whereas dyOptions() provides a way to fine tune appearance and behavior of the dygraph chart.
dygraph(ts_op) %>%
dySeries("ts1",label = "Product1") %>%
dySeries("ts2",label = "Product2") %>%
dyOptions(stackedGraph = TRUE) %>%
dyRangeSelector()
3 Series Options:
3.1 Step Plots : By default dygraphs displays series as a line, you can however plot series as step chart as follows:
dygraph(ts_op, main = "Sales comparison of product1 and product2") %>%
dySeries("ts1",label = "Product1") %>%
dySeries("ts2",label = "Product2") %>%
dyOptions(stepPlot = TRUE)
3.2 Point Display : You can include display of the individual points in a series as well as customize the size of the points.
dygraph(ts_op, main = "Sales comparison of product1 and product2") %>%
dySeries("ts1",label = "Product1") %>%
dySeries("ts2",label = "Product2") %>%
dyOptions(drawPoints = TRUE, pointSize = 2)
Series Highlighting
When users hover their mouse over series and points on the graph a highlight effect appears on the surface of the graph. You can use the dyHighlight function to customize how the highlighting appears.
In this example we specify a larger circle size for point highlighting as well as more decisively fade the non-highlighted series. We also request that the highlighting persist even after the mouse leaves the graph area.
dygraph(ts_op, main = "Sales comparison of product1 and product2") %>% dySeries("ts1",label = "Product1") %>% dySeries("ts2",label = "Product2") %>% dyHighlight(highlightCircleSize = 5, highlightSeriesBackgroundAlpha = 0.2, hideOnMouseOut = FALSE)
Likely we can also explore other features such as Labels & Legeds , Time Zone , Axis options etc.
Conclusion :
In a nutshell, the dygraphs package in R makes it a breeze to create awesome charts that you can interact with. You can easily plot time-series data, tweak how your chart looks, and even add extra details like prediction intervals. The best part? It’s super easy to explore your data – just zoom in, pan around, and highlight what matters most. With dygraphs, turning your data into insightful charts is both simple and fun!