Plotting a time series object
It is often very useful to plot data we are analyzing, as is the case when conducting time series analysis. If the dataset under study is of the ts
class, then the plot()
function has methods that automatically incorporate time index information into a figure.
Let's consider the eu_stocks
dataset (available in R by default as EuStockMarkets
). This dataset contains daily closing prices of major European stock indices from 1991-1998, specifically, from Germany (DAX
), Switzerland (SMI
), France (CAC
), and the UK (FTSE
). The data were observed when the markets were open, so there are no observations on weekends and holidays. We will proceed with the approximation that this dataset has evenly spaced observations and is a four dimensional time series.
To conclude this chapter, this exercise asks you to apply several of the functions you've already learned to this new dataset.
This exercise is part of the course
Time Series Analysis in R
Exercise instructions
- Use
is.ts()
to check whethereu_stocks
is a ts object. - View the start, end, and frequency of
eu_stocks
using thestart()
,end()
, andfrequency()
functions, respectively. - Generate a simple plot of your
eu_stocks
data using theplot()
command. - Generate a more complex time series plot of your
eu_stocks
data using thets.plot()
command. Input theeu_stocks
dataset into the pre-written code, but leave the other arguments as they are. - Use the pre-written code to add a legend to your time series plot.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Check whether eu_stocks is a ts object
# View the start, end, and frequency of eu_stocks
# Generate a simple plot of eu_stocks
# Use ts.plot with eu_stocks
ts.plot(___, col = 1:4, xlab = "Year", ylab = "Index Value", main = "Major European Stock Indices, 1991-1998")
# Add a legend to your ts.plot
legend("topleft", colnames(eu_stocks), lty = 1, col = 1:4, bty = "n")