dygraph()
is the main function to call
data : enter either time series data or data frame(numeric), for numeric data, the first column has to be the time.
Range selector dyRangeSelector()
: we can pipe this onto the original graph which let us select regions to look at
dySeries()
: display series and customize
dyOptions()
: specify color palette for series lines using colors option
+ `includeZero`
+ `axisLineColor`
+ `gridLineCOlor`
dyShading()
: shading from chose time periods
dyAxis()
:drawFrid, fillGraph options
time zones : dygraphs display time-series using time zone of the work machine. You can choose time zone withing the xts objset using useDataTimezone
option
Roll Periods dyRoller()
: smooth out the display of a series, plotted point representing the average of the number of the time stamps in roll periods.
We will use ts datasets::EuStockMarkets dataset to demonstrate using dygraphs package
Description
daily closing prices of major stock indeces: DAX, SMI, CAC and FTSE
1860 observations and 4 variables
mts object
from 1991 - 1998. The data are sampled in business time, i.e., weekends and holidays are omitted.
ts<-datasets::EuStockMarkets
kable(cbind(head(time(ts), 6), head(ts, 6)), format="pandoc")
DAX | SMI | CAC | FTSE | |
---|---|---|---|---|
1991.496 | 1628.75 | 1678.1 | 1772.8 | 2443.6 |
1991.500 | 1613.63 | 1688.5 | 1750.5 | 2460.2 |
1991.504 | 1606.51 | 1678.6 | 1718.0 | 2448.2 |
1991.508 | 1621.04 | 1684.1 | 1708.1 | 2470.4 |
1991.512 | 1618.16 | 1686.6 | 1723.1 | 2484.7 |
1991.515 | 1610.61 | 1671.6 | 1714.3 | 2466.8 |
#install.packages("dygraphs")
library(dygraphs);library(xts);
dt<-data.frame(date=time(ts),as.matrix(ts))
dt$date=round(dt$date, 3)
## our data is sorted if not you need to sort by dates
## load lubridate and xts
## data$datetime= ymd_hms(data$datetime)
## then xts(x= data$Y, order.by=data$datetime)
## logscale == TRUE , specify in axis , ignored if x is date format
dygraph(dt, main="European Stock Closing Price")%>%
dyRangeSelector()%>%dyLegend(show = "follow")%>%
dyAxis("x", axisLabelColor="black", axisLabelFontSize=1, axisLineColor="red")
dygraph(dt, main="European Stock Closing Price")%>%
dyRangeSelector()%>%dyLegend(show = "always")
dygraph(dt, main="European Stocks")%>%
#dySeries(label="DAX", color="black")%>%
dyLegend(show = "always", hideOnMouseOut = FALSE)%>%
dyShading(from="1995", to="1996", color="#FFE6E6") %>%
dyShading(from="1997", to="1998", color="#CCEBD6")
mean<-apply(dt[,-1], 2, mean)
std<-apply(dt[,-1], 2, sd)
lower=mean-std
upper=mean+std
dygraph(dt, main="European Stocks")%>%
dySeries(label="DAX", color="black") %>%
dyShading(from=lower[1], to=upper[1], color="lightblack", axis = "y")
dygraph(dt, main = "EU stock") %>%
dyAxis("y", label = "Stock Closing Price", valueRange = c(0, 10000)) %>%
dyOptions(axisLineWidth = 1.5,
fillGraph = TRUE,
drawGrid = TRUE,
colors = RColorBrewer::brewer.pal(4, "Dark2"),
includeZero = TRUE,
axisLineColor = "navy",
gridLineColor = "lightblue")
useDataTimezone
option##install.packages("xts")
library(xts)
ts.ran=seq.POSIXt(as.POSIXct("2019-01-01", tz="MST"),as.POSIXct("2019-07-02", tz="MST"), by="4 hours")
set.seed(123)
Y = rnorm(length(ts.ran))
## create time series object
serie= xts(Y, order.by = ts.ran, tz="MST")
dygraph(serie)%>%dyOptions(drawPoints = TRUE, pointSize = 2)%>%
dyRangeSelector()
useDataTimezone
optiondygraph(serie)%>%
dyRangeSelector()%>% dyOptions(useDataTimezone = TRUE, drawPoints = TRUE, pointSize = 2)
dyRoller()
In order to smooth out the series, we can use rollPerid
= a number you choose. Add a rolling average period text box to the bottom left of the plot. Y values are averaged over the specified number of time scale units(e.g. days, months, years)
dygraph(serie, main = "simulated time series") %>%
dyRangeSelector()%>% dyOptions(useDataTimezone = TRUE, drawPoints = TRUE, pointSize = 2)%>% dyRoller(rollPeriod = 6)
dygraph(serie, main = "simulated time series") %>%
dyRangeSelector()%>% dyOptions(useDataTimezone = TRUE, drawPoints = TRUE, pointSize = 2)%>%dyOptions( stemPlot=TRUE)
dygraph(serie, main = "simulated time series") %>%
dyRangeSelector()%>% dyOptions(useDataTimezone = TRUE, drawPoints = TRUE, pointSize = 2)%>%dyOptions( stepPlot=TRUE)