The ts_plot function

The plotting of time series object is most likely one of the steps of the analysis of time-series data. The \code{ is a customized function for plotting time series data based on the plotly package visualization engine. It supports the following time-series classes:

  • ts
  • mts
  • zoo
  • xts
  • data.frame1
  • tbl1

1 Must have a Date or POSIXct/lt column and at least on numeric column

For example let’s load and plot the USgas series, a ts object:


Customization options

It is straightforward to customize the plot using the different arguments of the ts_plot function

Adding titles

By default, the function sets the series name as the plot title. The title allows you to modify the plot title and the Xtitle and Ytitle enable you to add titles to the X-axis and Y-axis respectively:

Adding slider

The slider argument allows you to add a slider on the bottom of the plot, which will enable you to customize the window length of the plot:

Customize the line

The color, width arguments allow you to customize the line’s color and width of the plot:

The dash argument enables to create a dashed or dotted line (as opposed to the default setting of a solid line):

The line.mode enables to add to the solid line markers (by setting it to lines+markers) or just replace the solid one with markers (by setting it to markers):

Advance customization

The ts_plot is a wraper of the plotly package plotting functions for time series objects, therefore, the output of the ts_plot is a plotly object:

Advance customization of the ts_plot output can be done with plotly’s layout function. For example, let’s replot the USgas series and customize the background to black:

Note that the Xgrid and Ygrid arguments, when set to TRUE, add the corresponding X and Y grid lines.

Plotting multiple time series object

The plotting of a multiple time series object is straightforward. Let’s load the Coffee_Prices an mts object that represents the monthly prices of the Robusta and Arabica coffee prices (USD per Kg.):


By default, the function will plot all the series in one plot. Plotting the different series on a separate plot can be done by setting the type argument to multiple:

Note that the color, Ytitle, and Xtitle arguments are not applicable when plotting multiple time series object.

Working with other time series class

Working with other types of time series classes following the execute same process as the one demonstrated with the ts object above.

The main advantage of plotting time series objects such as xts, zoo, data.frame and now tsibble, is that their index is more readable (e.g., support Date and other time classes).

Plotting zoo and xts objects

Let’s load the University of Michigan consumer sentiment index:


Plotting data.frame object

Plotting data.frame or tbl objects must follow the following structure:

  • Have a Date or POSIXct/lt column
  • At least one numeric column

For example, let’s convert the USgas series to data.frame with the ts_to_prophet function: