dygraphs for R

dygraphs package is an R interface to the dygraphs JavaScript charting library. It provides rich facilities for charting time-series data in R,

#install.packages("dygraphs")
library(dygraphs)

Basic graph

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

Range selector

dygraph(lungDeaths) %>% 
  dyRangeSelector()

change data series level

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

Title and Y axis

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male") %>%
  dySeries("fdeaths", label = "Female") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)

Step Plot

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

##Point Display

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male") %>%
  dySeries("fdeaths", label = "Female") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)%>%
  dyOptions(drawPoints = TRUE, pointSize = 2)

##Point Shape

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male") %>%
  dySeries("fdeaths", label = "Female") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)%>%
  dyOptions(drawPoints = TRUE, pointSize = 2, pointShape = "triangle")

## Different line and point shape

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male",drawPoints = TRUE, pointSize = 2, pointShape = "triangle") %>%
  dySeries("fdeaths", label = "Female", stepPlot = TRUE, fillGraph = TRUE, color = "red") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)

Three Line graphs

lungDeaths <- cbind(mdeaths, fdeaths, ldeaths)
dygraph(lungDeaths, main = "Deaths from Lung Disease (UK)") 

## Different line and point shape

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male",drawPoints = TRUE, pointSize = 2, pointShape = "triangle") %>%
  dySeries("fdeaths", label = "Female", stepPlot = TRUE, fillGraph = TRUE, color = "red") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)

##Series Highlighting

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male",drawPoints = TRUE, pointSize = 2, pointShape = "triangle") %>%
  dySeries("fdeaths", label = "Female", stepPlot = TRUE, fillGraph = TRUE, color = "red") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20) %>% 
    dyHighlight(highlightCircleSize = 5, 
              highlightSeriesBackgroundAlpha = 0.2,
              hideOnMouseOut = FALSE)

Series Highlighting

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male",drawPoints = TRUE, pointSize = 2, pointShape = "triangle") %>%
  dySeries("fdeaths", label = "Female", stepPlot = TRUE, fillGraph = TRUE, color = "red") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20) %>% 
    dyHighlight(highlightCircleSize = 5, 
              highlightSeriesBackgroundAlpha = 0.2,
              hideOnMouseOut = FALSE,
              highlightSeriesOpts = list(strokeWidth = 3))

## Second Y Axis

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male",drawPoints = TRUE, pointSize = 2, pointShape = "triangle") %>%
  dySeries("fdeaths", label = "Female", stepPlot = TRUE, fillGraph = TRUE, color = "red", axis = 'y2') %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20) %>% 
    dyHighlight(highlightCircleSize = 5, 
              highlightSeriesBackgroundAlpha = 0.2,
              hideOnMouseOut = FALSE,
              highlightSeriesOpts = list(strokeWidth = 3))

dyCandlestick

library(xts)
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
data(sample_matrix)
m <- tail(sample_matrix, n = 32)
dygraph(m) %>%
  dyCandlestick()

##Annotation & Shading

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male",drawPoints = TRUE, pointSize = 2, pointShape = "triangle") %>%
  dySeries("fdeaths", label = "Female", stepPlot = TRUE, fillGraph = TRUE, color = "red", axis = 'y2') %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20) %>% 
    dyHighlight(highlightCircleSize = 5, 
              highlightSeriesBackgroundAlpha = 0.2,
              hideOnMouseOut = FALSE,
              highlightSeriesOpts = list(strokeWidth = 3))%>%
  dyAnnotation("1976-1-1", text = "Highest Value", width = 120)

##Annotation & Shading

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male",drawPoints = TRUE, pointSize = 2, pointShape = "triangle") %>%
  dySeries("fdeaths", label = "Female", stepPlot = TRUE, fillGraph = TRUE, color = "red", axis = 'y2') %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20) %>% 
    dyHighlight(highlightCircleSize = 5, 
              highlightSeriesBackgroundAlpha = 0.2,
              hideOnMouseOut = FALSE,
              highlightSeriesOpts = list(strokeWidth = 3))%>%
  dyAnnotation("1976-1-1", text = "Highest Value", width = 120) %>% 
  dyShading(from = "1975-1-1", to = "1977-1-1", color = "#FFE6E6")

##Events

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male",drawPoints = TRUE, pointSize = 2, pointShape = "triangle") %>%
  dySeries("fdeaths", label = "Female", stepPlot = TRUE, fillGraph = TRUE, color = "red", axis = 'y2') %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20) %>% 
    dyHighlight(highlightCircleSize = 5, 
              highlightSeriesBackgroundAlpha = 0.2,
              hideOnMouseOut = FALSE,
              highlightSeriesOpts = list(strokeWidth = 3))%>%
  dyShading(from = "1975-1-1", to = "1977-1-1", color = "#FFE6E6")%>%
  dyEvent("1976-2-1", "Highest Value", labelLoc = "bottom")

##Limits

dygraph(lungDeaths, main = "New Haven Temperatures", ylab = "Temp (F)") %>%
  dySeries("mdeaths", label = "Male",drawPoints = TRUE, pointSize = 2, pointShape = "triangle") %>%
  dySeries("fdeaths", label = "Female", fillGraph = TRUE, color = "red") %>%
  dyOptions(stackedGraph = TRUE) %>%
  dyRangeSelector(height = 20)%>%
  dyShading(from = "1975-1-1", to = "1977-1-1", color = "#FFE6E6")%>%
  dyEvent("1976-2-1", "Highest Value", labelLoc = "bottom")%>%
  dyLimit("3000",  "Average Value",color = "red")

Reference

  1. https://rstudio.github.io/dygraphs/