How to create better, interactive forecast plots using R and dygraph
By: Thomas Bierhance
Tags: Dygraph, R, Time Series Analysis, Forecasting, Data Science

If you are into R and time series you surely have stumbled across the great forecast package by Rob Hyndman. Apart from the various tools and methods for analyzing time series it also extends ggplot to visualize forecast objects using autoplot. While this yields really good results with almost no code, it does not provide interactive features. In this article you will learn…

how to plot a forecast with interactive features like zooming, panning and mouse-over information

how to include the 80% and 95% interval forecasts in the plot

how to write a JavaScript formatter to tweak the legend

This is what the result will look like…

Using autoplot for forecast objects

Let us look at the results of the basic autoplot function on forecasts objects first. Creating a 3-years forecast on the classic airline data set and plotting the result is straight forward with just three lines of code.

code that forecasts and plots the air passenger time series

autoplot of a forecast object

You can see the actual time series in black and the forecast in blue. The prediction intervals (80% and 95%) are also shown as blue areas. While this is certainly a good result for three lines of code, the result could be improved with interactive features.

Using dygraph for forecast objects

The JavaScript library dygraph can create interactive plots and an interface to this library for R is also available via the dygraphs package.

Converting our example from above to using dygraph just adds a line to bind the time series from the forecast object.

simple dygraph for a forecast object

basic dygraph showing actuals and point forecast

Now, while we got the interactive features of dygraph, suddenly the prediction intervals are missing. In dygraph a time series can be plotted with a so-called error bar to fix this. As only one error bar per time series is supported by dygraph, we just simply add one time series for each interval.

adding error bars for the interval forecasts

dygraph showing actuals, point and interval forecasts

That looks quite good, but there’s a little thing missing. If you hover over the graph, you will only see the values of the point forecast in the legend (569.93 in the figure above) and not the corresponding interval.

Writing a custom formatter for prediction intervals

The format of the values in the dygraph legend can be overwritten with a custom JavaScript function using the valueFormatter parameter of the dyAxis function. The JavaScript function will be called on mouse-over for each time series and will take six parameters:

num : the value on the mouse-over position of the current time series

: the value on the mouse-over position of the current time series opts : option values of the current dygraph

: option values of the current dygraph seriesName : name of the current time series

: name of the current time series g : the actual dygraph object

: the actual dygraph object row : the raw data’s row index for the current mouse-over position

: the raw data’s row index for the current mouse-over position col : the raw data’s column index for the current time-series

The value num that will be passed to the function does not include the interval values. However, the value for the given row and column index can be obtained from the dygraph object (function getValue ) and will contain this prediction interval. We can then format it as we like. Reusing the built-in Dygraph.numberValueFormatter function for formatting and passing the option values will keep the support for options like controlling the number of digits to display (see e.g. the option digitsAfterDecimal in dyOptions ).

Note that the value returned by dygraph will be a vector of length three even for time series with a single value like the actuals time series. Hence, I added a check if the upper and lower values actually differ before formatting as an interval.

custom JavaScript for formatting values in the legend

Furthermore, I applied some additional tweaks to the graph:

a dedicated time series for the point forecast to separate the interval forecasts from the point forecast in the legend

using the option labelsSeparateLines in the dyLegend function puts each label on a separate line, which makes the result more readable

in the function puts each label on a separate line, which makes the result more readable the dyRangeSelector function puts a nice selector on the bottom of the plot

function puts a nice selector on the bottom of the plot reducing the number of digits to one, using the option digitsAfterDecimal

a little CSS keeps the legend half-transparent to avoid covering the graph

final code using dygraph for plotting forecast objects plot

dygraph showing actuals, point and interval forecasts and custom legend

Thank you for reading!