The ts_plot is a versatile function for interactive plotting of time series objects. It is based on the plotly engine and supports multiple time series objects such as ts, mts, zoo, xts and as well the data frame family (data.frame, data.table, and tbl).
## [1] '0.1.4'
Plotting a univariate time series data with the ts_plot function is straightforward:
## The USgas series is a ts object with 1 variable and 227 observations
## Frequency: 12
## Start time: 2000 1
## End time: 2018 11
By default, the function will set the input object name as the plot title and leave the X and Y axises empty. It is possible to label the axises and set a different title:
There are several arguments which allow modifying the line main characteristics such as the colors, width, and type of line.
The line.mode argument is equivalent to the mode argument of the plot_ly function, and there are 3 options:
line - the default option, a clean lineline+markers - a line with a markersmarkers - only markersThe dash argument allows modifying the line to dashed or dotted by setting the argument to dash or dot respectively. The line width, by default, set to 2 and can be modified with the width argument:
ts_plot(USgas,
title = "US Natural Gas Consumption",
Xtitle = "Year",
Ytitle = "Billion Cubic Feet",
line.mode = "lines+markers",
width = 3,
color = "green")In addition, we can add grid lines for the Y and X axises and slider by setting the Xgrid, Ygrid and slider arguments to TRUE:
The ts_plot can handle multiple time series objects such as mts, xts, zoo and data frame family objects. The example below demonstrates the plot options for a multiple time series object with the closing prices of Apple, Facebook, Google and Microsoft stocks over time:
library(TSstudio)
library(xts)
library(zoo)
library(quantmod)
# Loading the stock price of key technology companies:
tckrs <- c("GOOGL", "FB", "AAPL", "MSFT")
getSymbols(tckrs,
from = "2013-01-01",
src = "yahoo"
)## [1] "GOOGL" "FB" "AAPL" "MSFT"
# Creating a multiple time series object
closing <- cbind(AAPL$AAPL.Close, FB$FB.Close, GOOGL$GOOGL.Close, MSFT$MSFT.Close)
names(closing) <- c("Apple", "Facebook", "Google", "Microsoft")
ts_info(closing)## The closing series is a xts object with 4 variables and 1581 observations
## Frequency: daily
## Start time: 2013-01-02
## End time: 2019-04-12
The type argument defines whatever to plot all the series in a single plot (single option) or plot each series separately (multiple option):
Currently, the ts_plot function supports three classes of data frame - data.frame, data.table and tbl. To be able to plot a data frame object, it must contain one column with a date or time object (Date or POSIXlt/POSIXct) and at least one numeric column. The US_indicators is an example of a data.frame object with time series data. It contains the monthly vehicle sales and the unemployment rate in the US since 1976 and a date object:
## 'data.frame': 517 obs. of 3 variables:
## $ Date : Date, format: "1976-01-31" "1976-02-29" ...
## $ Vehicle Sales : num 885 995 1244 1191 1203 ...
## $ Unemployment Rate: num 8.8 8.7 8.1 7.4 6.8 8 7.8 7.6 7.4 7.2 ...
The TSstudio package provides a set of visualizing applications for the forecasting process. Starting with visualizating the fitted and forecasted values vs. the actuals with the test_forecast function. In the example below, we will use the ts_split function to split the USgas dataset into a training and testing partitions, leaving the last 12 months as testing:
USgas_partition <- ts_split(ts.obj = USgas, sample.out = 12)
train <- USgas_partition$train
test <- USgas_partition$test
library(forecast)
md1 <- auto.arima(train)
fc1 <- forecast(md1, h = 12)
test_forecast(actual = USgas, forecast.obj = fc1, test = test)You can hover over the plot to review the MAPE and RMSE of the model over the training and testing partitions.
In addition, the plot_forecast function visualize any forecast object from the forecast package or bsts forecast object from the bsts package:
An alternative approach for the plot_forecast function, which provides a traditional forecast plot with confidence intervals, is to perform forecasting simulation based on the model distribution (using the simulate function) and plot it. The forecast_sim function provides this functionality. Let’s take the md2, the auto.arima forecasting model, and simulate 200 forecasting path with the horizon of 60 months: