Plotting Financial Time Series Data (Multiple Columns) in R

Let's take one more example of plotting financial time series data. This time we will use the EuStockMarkets data set that comes by default with R. It contains the daily closing prices of major European stock indices from 1991 to 1998.

Check and Print the Data

Let's first check if the data is a time series and then print a few values from the dataset to get a feel of the data.

> is.ts(EuStockMarkets)
[1] TRUE
> head(EuStockMarkets)
         DAX    SMI    CAC   FTSE
[1,] 1628.75 1678.1 1772.8 2443.6
[2,] 1613.63 1688.5 1750.5 2460.2
[3,] 1606.51 1678.6 1718.0 2448.2
[4,] 1621.04 1684.1 1708.1 2470.4
[5,] 1618.16 1686.6 1723.1 2484.7
[6,] 1610.61 1671.6 1714.3 2466.8
>

As you can see, the dataset is a time series object and contains prices for 4 indices, namely, DAX, SMI, CAC and FTSE.

Plot the Data

We can plot the data using the plot() function to create a simple plot which will print all 4 series as shown below:

> plot(EuStockMarkets)

Alternatively we can use the ts.plot() function to get the same plot but with a common y-axis. This time we are also adding a legend to the chart to make it more user-friendly.

> ts.plot(EuStockMarkets, col = 1:4, xlab = "Year", ylab = "Prices", main = "European Stock Indices")
> legend("topleft", colnames(EuStockMarkets), lty = 1, col = 1:4, bty = "n")
>

Notes: col sets the colors of lines, lty is the line type, and bty is for the type of box to be drawn around the legend. n represents no box.

Related Downloads

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book includes PDFs, explanations, instructions, data files, and R code for all examples.

Get the Bundle for $29 (Regular $57)
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book comes with PDFs, detailed explanations, step-by-step instructions, data files, and complete downloadable R code for all examples.