11.2 dygraphs package
The package dygraphs produces dynamic graphics so that user can interact with the graph.
To illustrate our idea, we use stock data download using Quantmod package.
11.2.1 Get Data Using Quantmod
The following code install and download the quantmod package. Then it downloads the daily stock price data of Apple (ticker: AAPL).
We use getSymbols() to download data:
Take a look at the data:
## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2007-01-03 3.081786 3.092143 2.925000 2.992857 1238319600 2.573566
## 2007-01-04 3.001786 3.069643 2.993571 3.059286 847260400 2.630688
## 2007-01-05 3.063214 3.078571 3.014286 3.037500 834741600 2.611954
## AAPL.Open AAPL.High AAPL.Low AAPL.Close
## 2007-01-03 3.081786 3.092143 2.925000 2.992857
## 2007-01-04 3.001786 3.069643 2.993571 3.059286
## 2007-01-05 3.063214 3.078571 3.014286 3.037500
The following code install and download the dygraphs package.
We will plot four different dynamic plots:
- standard dynamic,
- shading,
- event line, and
- candle chart.
11.2.2 Standard dynamic graph
The function dygraph() display time series data interactively. Move your mouse on the diagram would show prices
11.2.3 Event line
We want to decorate the dynamic graph by a line to indicate one-day event. To do it, we do it line by line using the function dyEvent() puting the date information, label, and location of the label.
11.2.4 Shading
Event sometimes lasts more one day. Then we would like to highlight the whole period of time. Usually, highlighting over economic recession period is common in economic graphics. It can be used to mark periods when the market is bearish.