Get startedGet started for free

Different ways of plotting risk-factor and return series

You already know that you can use plot.zoo() to plot multiple time series. For a four-dimensional time series data, the call plot.zoo(data) creates four separate plots by default, unless you include the parameter plot.type = "single" to plot all four series in one plot. You can also add even more parameters such as col to specify different colors and type = "h" to get vertical bars instead of joining points, which can sometimes be a better way of displaying returns.

plot.zoo(x, plot.type, col = 1, type = "l", ...)

In this exercise, you will explore the plot.zoo() function to plot equity risk-factor data and the corresponding returns in different ways. The multivariate time series djstocks and DJ_const are available in your workspace.

This exercise is part of the course

Quantitative Risk Management in R

View Course

Exercise instructions

  • Plot djstocks in four separate plots.
  • Plot djstocks in one plot in colors 1 to 4. The code to create an appropriate legend for the plot is provided.
  • Compute the log-returns of djstocks and assign them to djstocks_x.
  • Plot djstocks_x in four separate plots.
  • Plot djstocks_x in four separate plots with vertical bars.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Plot djstocks in four separate plots
___(___)

# Plot djstocks in one plot and add legend
___(___, ___, ___)
legend(julian(x = as.Date("2009-01-01")), y = 70, legend = names(DJ_const)[1:4], fill = 1:4)

# Compute log-returns and assign to djstocks_x
djstocks_x <- ___(___)

# Plot djstocks_x in four separate plots
___(___)

# Plot djstocks_x with vertical bars
___(___, ___)
Edit and Run Code