Interactive time series plots in R

For our purposes though, I wanted to illustrate how dygraphs can add additional tools, such as a second y axis (which probably obfuscates things with our test data set, but I wanted to make sure I included this code so I could come back to it later when it was appropriate!), a range selector and an averaging window option.# create plot using dygraphlibrary(dygraphs)# basic chartdygraph(stocks)# customised chartlibrary(dplyr)stock_dyg <- dygraph(stocks, main = "AAPL and MSFT Closing Price 1st Jan – 30th Nov 2018") %>% dySeries("MSFT.Close", axis = "y2") %>% dyAxis("y", label = "AAPL") %>% dyAxis("y2", label = "MSFT", valueRange = c(80, 120), independentTicks = TRUE) %>% dyRangeSelector(dateWindow = c("2018-01-01", "2018-11-30")) %>% dyRoller()stock_dyg# save as htmllibrary(htmlwidgets)saveWidget(stock_dyg, "stock_dy.html", selfcontained = TRUE)We now have a chart where we can zoom in (double click to zoom back out), smooth the peaks and troughs with our averaging window and get the raw data for each day by hovering..In R Studio the output is rendered immediately in the viewer or, in this case, we’ve saved the output as html using the htmlwidgets package.Happy interactive graphing and dashboarding!. More details

Leave a Reply