How to Use the tsplot() Function in R


Often you may want to plot the values from a time series in R.

One of the easiest ways to do so is by using the tsplot() function from the astsa package in R.

This function uses the following basic syntax:

tsplot(x, y=NULL, main=NULL, ylab=NULL, xlab=’Time’, …)

where:

  • x, y: Time series to be plotted
  • main: Title to use in plot
  • ylab: Label to be used for y-axis
  • xlab: Label to be used for x-axis (default is ‘Time’)

Note that you can use any arguments in the tsplot() function that can also be used with any plot in base R to customize the plot axis labels, margins, etc.

The following example shows how to use the tsplot() function to produce time series plots in practice.

Example: How to Use tsplot() Function in R

Suppose we create the following dataset in R that contains information about sales made on various dates at some store:

#make this example reproducible
set.seed(12)

#create dataset
df <- data.frame(date = as.Date("2024-01-01") - 0:99,
                 sales = runif(100, 10, 500) + seq(50, 149)^2)

#view head of dataset
head(df)

        date    sales
1 2024-01-01 2543.987
2 2023-12-31 3011.710
3 2023-12-30 3175.885
4 2023-12-29 2950.997
5 2023-12-28 3008.981
6 2023-12-27 3051.609

Note: We used set.seed() to ensure that each time we run this code, the same set of random values is generated in the dataset.

Suppose that we would like to create a time series plot to visualize the sales over time.

We can use the tsplot() function to do so.

Note that you may need to first run the following code to install the astsa package in R if it is not already installed:

install.packages('astsa')

Once the package is installed, we can then use the following syntax to create a basic time series plot using the tsplot() function:

library(astsa)

tsplot(df$sales)

This produces the following time series plot:

tsplot in R

The x-axis displays the time while the y-axis displays the values from the sales column of the data frame.

Since we didn’t specify a value to use for the x-axis, the tsplot() function simply generated a range of ‘time’ values ranging from 0 to 100.

To use the values from the date column of the data frame as the values for the x-axis, we must explicitly include the date column in the tsplot() function as follows:

library(astsa)

#create time series plot of date vs. sales
tsplot(df$date, df$sales)

This produces the following time series plot:

The x-axis now uses the range of actual dates from the date column of the data frame.

Note that we can also use the xlab, ylab and main arguments to customize the x-label, y-label and title of the plot.

We can use the following syntax to do so:

library(astsa)

#create time series plot of date vs. sales with custom labels
tsplot(df$date, df$sales, main='Date vs Sales', xlab='Date', ylab='Sales')

Notice that the x-label, y-label and title are now all customized just as we specified.

Feel free to use whatever custom labels you would like for your own time series plot.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

A Complete Guide to the Best ggplot2 Themes
The Complete Guide to ggplot2 Titles
How to Create Side-by-Side Plots in ggplot2

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *