How to use interactive time series graph using dygraphs in R
Creating simple dygraphs
Let’s take stock data for a better understanding of time series.
Download (Tesla) Stock data from the quantmod library by using getSymbols(). We will plot 5 different dynamic plots.
- Series colour
- Vertical and Horizontal Shading
- Upper/lower bar
- Range Selector
First of all, install dygraphs and quantmod package in rstudio.
You can download data directly from this link: TSLA Stock Data
Here, OHLC represents the open, high, low, and closing price of each period in stock data. So, we are fetching this parameter and storing it in the price variable.
Series color graph
Dygraph can assign different color palettes to each visualization line. For example, our four-parameter in TSLA data (open, high, low, close) can be represented as different colors. Dygraph provides dyOptions() in which colors parameter can be useful to identify each stock line smoothly.
Vertical and Horizontal shading
Dygraph can assign shading features on the timeline portion to analyze different time intervals. By using dyshading, we can assign time-interval corresponding to color for better analysis of the stock portion.
Dygraphs provide horizontal shading by use of mean and standard deviations. We can assign intervals in dyshading and assign axis parameters to y. Here, ROC function is used to create ROC Curve.
Candlestick graphs are most commonly used by professional traders in the stock market to predict price movement based on past data. dygraph can easily plot this graph just in one line of command. Here, we are storing the last 30 days stock data to create a candlestick graph. Red bar shows down in stock and the green bar shows up in stock.
We can create multiple series in which each series has an upper/lower bar in terms of shade just like the error bar. In the Below graph, the amazon and tesla series example is shown.
We can add a range selector at the bottom of the dygraph chart for an easy interface of panning and zooming by dyRangeSelector() function.