3.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.
3.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:
install.packages("quantmod")
library(quantmod)
# getSymbols(): to get data
getSymbols("AAPL")
Take a look at the data:
# OHLCVA data
head(AAPL,n=3)
## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
## 2003-01-02 0.4816753 0.5004593 0.4813400 0.4964343 45357200
## 2003-01-03 0.4964343 0.5007950 0.4893903 0.4997887 36863400
## 2003-01-06 0.5041489 0.5158889 0.4991176 0.4997887 97633200
## AAPL.Adjusted
## 2003-01-02 0.704906
## 2003-01-03 0.709669
## 2003-01-06 0.709669
# Get OHLC data
price<-OHLC(AAPL)
head(price, n=3)
## AAPL.Open AAPL.High AAPL.Low AAPL.Close
## 2003-01-02 0.4816753 0.5004593 0.4813400 0.4964343
## 2003-01-03 0.4964343 0.5007950 0.4893903 0.4997887
## 2003-01-06 0.5041489 0.5158889 0.4991176 0.4997887
The following code install and download the dygraphs package.
install.packages("dygraphs")
library(dygraphs)
getSymbols("AAPL")
getSymbols("SPY")
We will plot four different dynamic plots:
- standard dynamic,
- shading,
- event line, and
- candle chart.
3.2.2 Standard dynamic graph
The function dygraph() display time series data interactively. Move your mouse on the diagram.
dygraph(OHLC(AAPL))
3.2.3 Shading
graph<-dygraph(Cl(SPY), main = "SPY")
dyShading(graph, from="2007-08-09",
to="2011-05-11", color="#FFE6E6")
3.2.4 Event line
graph<-dygraph(OHLC(AAPL), main = "AAPL")
graph<-dyEvent(graph,"2007-6-29",
"iphone", labelLoc = "bottom")
graph<-dyEvent(graph,"2010-5-6",
"Flash Crash", labelLoc = "bottom")
graph<-dyEvent(graph,"2014-6-6",
"Split", labelLoc = "bottom")
dyEvent(graph,"2011-10-5",
"Jobs", labelLoc = "bottom")
3.2.5 Candle Chart
AAPL <- tail(AAPL, n=30)
graph<-dygraph(OHLC(AAPL))
dyCandlestick(graph)