Basics

We will use ts datasets::EuStockMarkets dataset to demonstrate using dygraphs package

EuStockMarkets

Description

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

Exercise step by step

range selecting that allows zooming

#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")

always show legends

dygraph(dt, main="European Stock Closing Price")%>%
dyRangeSelector()%>%dyLegend(show = "always")

Shading and Annotations + Vertical Shading

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")

horizontal shading

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") 

Axis and Options

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")

Exercise step by step

before use useDataTimezone option

does not plot in the correct zone

does not plot in the correct zone

##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()

after use useDataTimezone option

Correct!

Correct!

dygraph(serie)%>%
dyRangeSelector()%>% dyOptions(useDataTimezone = TRUE, drawPoints = TRUE, pointSize = 2)

More

Roll Periods 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)

stemPlot

dygraph(serie, main = "simulated time series") %>%
dyRangeSelector()%>% dyOptions(useDataTimezone = TRUE, drawPoints = TRUE, pointSize = 2)%>%dyOptions( stemPlot=TRUE)

stepPlot

dygraph(serie, main = "simulated time series") %>%
dyRangeSelector()%>% dyOptions(useDataTimezone = TRUE, drawPoints = TRUE, pointSize = 2)%>%dyOptions( stepPlot=TRUE)