Time Series Plots in R With Examples

In this tutorial, we’ll be going over how to create time series plots in R. Time series data refers to data points that represent a particular variable changing over different points of time. It can be thought of as a sequence of data that was recorded at regular time intervals.

Time series data is widely used in stock market analysis, weather analysis, market trend analysis and any other scenarios where data variations with time are important.

R has several packages to perform time-series plotting and analysis tasks. Let us begin by acquiring some standard time series data for our work.

Acquiring Data

Several data scientists and organizations have open-sourced time series datasets that could be directly downloaded to the R environment. Two of these sources are:

The packages can be installed into your R environment using install.packages("packagename") command. Other relevant instructions are present on the websites give above.

Let us proceed with some data from the tsdl package for illustrating time series plotting.

Viewing Time Series Data

The tsdl package has numerous data series across several categories. Let us try accessing some of these sets. The first step is to load the package into memory.

Let us try choosing a time-series for our plotting. We first create a subset of the above dataset using the subset function for the respective category.

Now, in order to access the time series, we need to index the data frame created above. This particular time series represents the number of monthly armed robberies in Boston from Jan 1965 to Oct 1977.

We now create a time series object from this data frame using the function.

The ts() function converts a numeric vector into a time series object. The syntax is as follows:

You can choose to convert only a part of the time series instead of the whole series by selecting the start and endpoints from the whole series.

We can retrieve only the crime data from 1970 January to 1972 December using the following command:

The frequency option indicates how often the observations are to be made. 1 indicates annual, 4 indicates quarterly and so on. By default, frequency takes one observation per year by calculating the mean of all observations.

If we need more fine-grained observations, we need to specify 12 as the frequency (one observation every month).

Creating Time Series Plots in R

R provides plot.ts() function to plot time-series graphs. Let us re-examine our series data.

Since this series was not specified with a start and end date, the plot will just display the observation number instead of the year number.


A plain time-series graph with no years

We are now going to redefine the series object with starting and ending dates and frequency set to 12.


Decomposing Time Series

It is possible to further analyze the time series by using decomposition. These additional pieces of information can be separately plotted as 3 different plots along with the observed plot:

  • Seasonal: How patterns repeat over certain intervals of time
  • Trend: The general direction of the time series progress – whether rising or falling.
  • Random: The inherent irregularity present in the data when the trend and seasonality are removed.

This information can be derived from a series using the decompose() function as follows.

The result is a list of all the above components of the series. These can be plotted using a plot() function directly.


From the graph, it can be observed that there is a seasonality in the crimes being performed, and the trend is generally on the rise.

Time series plots are an important means of data analysis for sequential and time-varying data. R functionalities like those mentioned above make the tasks easier.

By admin

Leave a Reply

%d bloggers like this: