dygraphs for R

library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths)

#--------------------------------------
lungDeaths <- cbind(ldeaths, mdeaths, fdeaths)
dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyOptions(colors = RColorBrewer::brewer.pal(3, "Set2"))

#--------------------------------------------
#Step Plots
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyOptions(stepPlot = TRUE)

#---------------------------------------------
#Filling
dygraph(ldeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyOptions(fillGraph = TRUE, fillAlpha = 0.4)

#-------------------------------------------
#Point Display
dygraph(ldeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyOptions(drawPoints = TRUE, pointSize = 2)

#-----------------------------------------------
#Per-Series Options
dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dySeries("mdeaths", drawPoints = TRUE, color = "blue") %>%
  dySeries("fdeaths", stepPlot = TRUE, fillGraph = TRUE, 
           color = "red")

#-----------------------------------------------
#Line Strokes
dygraph(ldeaths, main = "Deaths from Lung Disease (UK)") %>%
  dySeries("V1", strokeWidth = 2, strokePattern = "dashed")

#---------------------------------------------------

dygraph(lungDeaths) %>% dyRangeSelector()

#----------------------------------------------
dygraph(lungDeaths) %>%
  dySeries("mdeaths", label = "Male") %>%
  dySeries("fdeaths", label = "Female") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)

#--------------------------------
hw <- HoltWinters(ldeaths)
predicted <- predict(hw, n.ahead = 72, 
                     prediction.interval = TRUE)
#----------------------------------------------------
dygraph(predicted, main = "Predicted Lung Deaths (UK)") %>%
  dyAxis("x", drawGrid = FALSE) %>%
  dySeries(c("lwr", "fit", "upr"), label = "Deaths") %>%
  dyOptions(colors = RColorBrewer::brewer.pal(3, "Set1"))

——————-!!!!!!!!!!!!!!!

#Series Highlighting



dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyHighlight(highlightSeriesOpts = list(strokeWidth = 3))

#-------------------------------------------------
#Second Y-Axis
# define mts with distinct y-axis scales
temperature <- ts(frequency = 12, start = c(1980, 1),
                  data = c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 
                           25.2, 26.5, 23.3, 18.3, 13.9, 9.6))
rainfall <- ts(frequency = 12, start = c(1980, 1),
               data = c(49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 
                        135.6, 148.5, 216.4, 194.1, 95.6, 54.4))
weather <- cbind(rainfall, temperature)

# assign the "rainfall" series to the y2 axis
dygraph(weather) %>%
  dySeries("rainfall", axis = 'y2')

dygraph(weather) %>%
  dyAxis("y", label = "Temperature (C)") %>%
  dyAxis("y2", label = "Rainfall", independentTicks = TRUE) %>%
  dySeries("rainfall", axis = 'y2')

#--------------------------------------------------
#Plot Labels
dygraph(discoveries, 
        main = "Important Discoveries", 
        ylab = "Discoveries / Year")

dygraph(discoveries, main = "Important Discoveries") %>%
  dyAxis("y", label = "Discoveries / Year")

#--------------------------------------------------
#Plot Legend
dygraph(nhtemp, main = "New Haven Temperatures") %>% 
  dySeries("V1", label = "Temperature (F)") %>%
  dyLegend(show = "always", hideOnMouseOut = FALSE)

lungDeaths <- cbind(ldeaths,mdeaths,fdeaths)
dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") %>%
  dyLegend(width = 400)

#—————————————————-

#Range Selector
dygraph(nhtemp, main = "New Haven Temperatures") %>% 
  dyRangeSelector()

dygraph(nhtemp, main = "New Haven Temperatures") %>% 
  dyRangeSelector(dateWindow = c("1920-01-01", "1960-01-01"))

dygraph(nhtemp, main = "New Haven Temperatures") %>% 
  dyRangeSelector(height = 20, strokeColor = "")

#----------------------------------------------------
#Synchronization
dygraph(ldeaths, main = "All", group = "lung-deaths")

dygraph(mdeaths, main = "Male", group = "lung-deaths")

dygraph(fdeaths, main = "Female", group = "lung-deaths")

#----------------------------------------------------
#Roll Periods
dygraph(discoveries, main = "Important Discoveries") %>% 
  dyRoller(rollPeriod = 5)

#--------------------------------------------------
#Shaded Regions
dygraph(nhtemp, main = "New Haven Temperatures") %>% 
  dyShading(from = "1920-1-1", to = "1930-1-1") %>%
  dyShading(from = "1940-1-1", to = "1950-1-1")

dygraph(nhtemp, main = "New Haven Temperatures") %>% 
  dySeries(label = "Temp (F)", color = "black") %>%
  dyShading(from = "1920-1-1", to = "1930-1-1", color = "#FFE6E6") %>%
  dyShading(from = "1940-1-1", to = "1950-1-1", color = "#CCEBD6")

library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
#getSymbols("MSFT"), from = "2014-06-01", auto.assign=TRUE)

quantmod::getSymbols("MSFT", from = "2014-06-01", auto.assign=TRUE)
##     As of 0.4-0, 'getSymbols' uses env=parent.frame() and
##  auto.assign=TRUE by default.
## 
##  This  behavior  will be  phased out in 0.5-0  when the call  will
##  default to use auto.assign=FALSE. getOption("getSymbols.env") and 
##  getOptions("getSymbols.auto.assign") are now checked for alternate defaults
## 
##  This message is shown once per session and may be disabled by setting 
##  options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.
## Warning in if (as.character(sc[[1]]) != calling.fun) return(): the
## condition has length > 1 and only the first element will be used
## [1] "MSFT"
ret = ROC(MSFT[, 4])
mn = mean(ret, na.rm = TRUE)
std = sd(ret, na.rm = TRUE)
dygraph(ret, main = "Microsoft Share Price") %>% 
  dySeries("MSFT.Close", label = "MSFT") %>%
  dyShading(from = mn - std, to = mn + std, axis = "y")

————————————————-

#Event and Limit Lines
dygraph(presidents, main = "Quarterly Presidential Approval Ratings") %>%
  dyAxis("y", valueRange = c(0, 100)) %>%
  dyEvent(date = "1950-6-30", "Korea", labelLoc = "bottom") %>%
  dyEvent(date = "1965-2-09", "Vietnam", labelLoc = "bottom")

library(quantmod)
#getSymbols("MSFT"), from = "2014-06-01", auto.assign=TRUE)

quantmod::getSymbols("MSFT", from = "2014-06-01", auto.assign=TRUE)
## Warning in if (as.character(sc[[1]]) != calling.fun) return(): the
## condition has length > 1 and only the first element will be used
## [1] "MSFT"
dygraph(MSFT[, 4], main = "Microsoft Share Price") %>% 
  dySeries("MSFT.Close", label = "MSFT") %>%
  dyLimit(as.numeric(MSFT[1, 4]), color = "red")

#Annotations
dygraph(presidents, main = "Quarterly Presidential Approval Ratings") %>%
  dyAxis("y", valueRange = c(0, 100)) %>%
  dyAnnotation("1950-7-1", text = "A", tooltip = "Korea") %>%
  dyAnnotation("1965-1-1", text = "B", tooltip = "Vietnam")

presAnnotation <- function(dygraph, x, text) {
  dygraph %>%
    dyAnnotation(x, text, attachAtBottom = TRUE, width = 60)
}

dygraph(presidents, main = "Quarterly Presidential Approval Ratings") %>%
  dyAxis("y", valueRange = c(0, 100)) %>%
  presAnnotation("1950-7-1", text = "Korea") %>%
  presAnnotation("1965-1-1", text = "Vietnam")

#Upper/Lower Bars
hw <- HoltWinters(ldeaths)
p <- predict(hw, n.ahead = 72, prediction.interval = TRUE)

dygraph(p, main = "Predicted Lung Deaths (UK)") %>%
  dySeries(c("lwr", "fit", "upr"), label = "Deaths")

hw <- HoltWinters(ldeaths)
p <- predict(hw, n.ahead = 36, prediction.interval = TRUE)
all <- cbind(ldeaths, p)

dygraph(all, "Deaths from Lung Disease (UK)") %>%
  dySeries("ldeaths", label = "Actual") %>%
  dySeries(c("p.lwr", "p.fit", "p.upr"), label = "Predicted")

library(quantmod)
getSymbols(c("MSFT", "HPQ"), from = "2014-06-01", auto.assign=TRUE)
## [1] "MSFT" "HPQ"
stocks <- cbind(MSFT[,2:4], HPQ[,2:4])
dygraph(stocks, main = "Microsoft and HP Share Prices") %>% 
  dySeries(c("MSFT.Low", "MSFT.Close", "MSFT.High"), label = "MSFT") %>%
  dySeries(c("HPQ.Low", "HPQ.Close", "HPQ.High"), label = "HPQ")