Plotting Time Series Data

Rami Krispin (@Rami_Krispin)

2018-09-17

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).

# install.packages("TSstudio")
library(TSstudio)
packageVersion("TSstudio")
## [1] '0.1.3'
# Function documentation
?ts_plot

Plotting a univariate time series

Plotting a univariate time series data with the ts_plot function is straightforward:

# Loading the USgas dataset
data("USgas")

ts_info(USgas)
##  The USgas series is a ts object with 1 variable and 225 observations
##  Frequency: 12 
##  Start time: 2000 1 
##  End time: 2018 9
ts_plot(USgas)

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:

# Setting the plot titles
ts_plot(USgas,
        title = "US Natural Gas Consumption",
        Xtitle = "Year",
        Ytitle = "Billion Cubic Feet")

Line setting

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:

  1. line - the default option, a clean line
  2. line+markers - a line with a markers
  3. markers - only markers

The 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:

ts_plot(USgas,
        title = "US Natural Gas Consumption",
        Xtitle = "Year",
        Ytitle = "Billion Cubic Feet",
        line.mode = "lines+markers",
        width = 1,
        color = "#66C2A5",
        Xgrid = TRUE,
        Ygrid = TRUE,
        slider = TRUE)

Ploting a multiple time series object

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 1486 observations
##  Frequency: daily 
##  Start time: 2013-01-02 
##  End time: 2018-11-23

The type argument defines whatever to plot all the series in a single plot (single option) or plot each series separately (multiple option):

# Plot each series in a sepreate (default option)
ts_plot(closing,
        title = "Top Technology Companies Stocks Prices Since 2013", 
        type = "multiple")
# All the series in one plot
ts_plot(closing, 
        title = "Top Technology Companies Stocks Prices Since 2013",
        type = "single")

Plotting data frame objects

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(US_indicators)
str(US_indicators)
## 'data.frame':    514 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 ...
ts_plot(US_indicators)