For our examples, we’ll be utilizing the ‘Seatbelts’ package that’s built into R to demonstrate dygraphs. This package includes monthly data about vehicle injuries in Great Britain from January 1969 to December 1984. Since the data can be typified as a time series and there are multiple variables suitable for comparison, this data is fit for dygraphs usage.
The primary function in dygraphs is dygraph(). A great deal of the other functions in dygraphs simply serve as additions or modifications to dygraph().
To demonstrate the ease with which you can use dygraphs, Here is dygraph() with two columns from Seatbelts (front and rear, indicating injuries sustained in the front row or back rows) as its inputs:
seatbelts=as.xts(Seatbelts)
seatbelts1<- cbind(seatbelts$front,seatbelts$rear)
dygraph(seatbelts1, main="Monthly Injuries/Deaths by Passenger Row")
After coercing the data into an xts object, the only arguments here are the data and a title (using ‘main=’). Without much work, we have a functional, interactive, and easy-to-read time series plot with our variables of interest distinguished by color. As the cursor moves along the plot, its data values are shown in the top right.
This example builds upon the prior example. dyRangeSelector allows the user to zoom in, then double-click to return to the full data. dyLimit, which marks a horizontal point, is included here as well.
Also shown is the similar ‘dyEvent()’ function which marks a vertical point. In time series, this is helpful because you are able to note events that may or may not change the data trend going forward. In this example, the Event line signifies when wearing a seat belt became compulsory in Great Britain.
Though the added range selector allows the user to zoom in, any dygraphs chart automatically allows the viewer to zoom by clicking & dragging your mouse. If you’d like to pan from a zoomed view, just shift + drag your mouse.
dygraph(seatbelts1, main="Monthly Injuries/Deaths by Passenger Row") %>%
dyAxis('y',label='Deaths or Serious Injuries') %>%
dyAxis('x',label='Time', drawGrid = FALSE)%>%
dyRangeSelector()%>% #Range selector-- allows user to zoom in.. Double click to return to full-page.
dyLimit(450, color = "red")%>% #add limit line
dyEvent('1983-2-01', "Law introduced ", labelLoc = 'top', color='blue')#add event line in blue
Here is another Seatbelts plot, showing the number of drivers killed each month. To demonstrate some of the stylistic/cosmetic options available with dygraphs, the color has been changed, the point shape has been changed to a square, and the stroke width has been made bigger using dyOptions().
dygraph(seatbelts$DriversKilled, main='Drivers Killed')%>%
dyAxis('y',label='Number of drivers killed') %>%
dyAxis('x',label='Time', drawGrid = FALSE)%>%
dyHighlight(highlightSeriesBackgroundAlpha = 0.1,
hideOnMouseOut = TRUE)%>%
dyOptions(pointShape = 'square',
strokeWidth = 2,
colors = "hotpink")
In this example, we add dyCandlestick() argument to a dygraphs function. This candlestick chart differs from normal time series plots because the data points for each date are not connected; instead, each point is represented by a vertical bar.
Note: this function might be more suitable for a dataset where you want to compare 4 variables (for example, taking a look at the open, close, high, and low of a stock price).
seatbelts2<- cbind(seatbelts$DriversKilled,
seatbelts$front,
seatbelts$rear,
seatbelts$VanKilled)
dygraph(seatbelts2,main='Seatbelts Candlestick Chart')%>%
dyAxis('y',label='Deaths or Serious Injuries') %>%
dyAxis('x',label='Time', drawGrid = FALSE)%>%
dyCandlestick() %>%
dyRangeSelector() %>%
dyEvent('1983-2-01', labelLoc = 'top', color='blue')
To demonstrate the dyRibbon() function, we’ve taken an altered version of the data (kms and DriversKilled*10) and compared them for the purposes of demonstration. dyRibbon allows the creator to give colored cues about the data. Here, where the kilometers line is increasing, the ribbon is green; where decreasing, the ribbon is red.
difference <- seatbelts$kms -(100*seatbelts$DriversKilled)
decreasing <- which(difference < 0)
increasing <- which(difference > 0)
dyData <- c(seatbelts$kms)
ribbonData <- rep(0, nrow(dyData))
ribbonData[decreasing] <- 0.1
ribbonData[increasing] <- 1
dygraph(dyData, main="Distance Driven") %>%
dyRibbon(data = ribbonData,top=.83,bottom=.5) %>%
dyAxis('y',label='Distance (km)') %>%
dyAxis('x',label='Time', drawGrid = FALSE)%>%
dyOptions(strokeWidth = 2, colors='navy')