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:
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:
It is straightforward to customize the plot using the different arguments of the ts_plot function
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:
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:
The color
, width
arguments allow you to customize the line’s color and width of the plot:
ts_plot(USgas,
title = "US Monthly Natural Gas Consumption",
Xtitle = "Time",
Ytitle = "Billion Cubic Feet",
color = "black",
width = 3)
The dash
argument enables to create a dashed or dotted line (as opposed to the default setting of a solid line):
ts_plot(USgas,
title = "US Monthly Natural Gas Consumption",
Xtitle = "Time",
Ytitle = "Billion Cubic Feet",
dash = "dash")
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
):
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:
library(plotly)
ts_plot(USgas,
title = "US Monthly Natural Gas Consumption",
Xtitle = "Time",
Ytitle = "Billion Cubic Feet",
color = "pink",
Xgrid = TRUE,
Ygrid = TRUE) %>%
layout(paper_bgcolor = "black",
plot_bgcolor = "black",
font = list(color = "white"),
yaxis = list(linecolor = "#6b6b6b",
zerolinecolor = "#6b6b6b",
gridcolor= "#444444"),
xaxis = list(linecolor = "#6b6b6b",
zerolinecolor = "#6b6b6b",
gridcolor= "#444444"))
Note that the Xgrid
and Ygrid
arguments, when set to TRUE
, add the corresponding X and Y grid lines.
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.):
data("Coffee_Prices")
ts_info(Coffee_Prices)
#> The Coffee_Prices series is a mts object with 2 variables and 701 observations
#> Frequency: 12
#> Start time: 1960 1
#> End time: 2018 5
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 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).
Let’s load the University of Michigan consumer sentiment index: