Highlighting a specific period in a time series
To highlight a specific period in a time series, you can display it in the plot in a different background color. The chart.TimeSeries()
function in the PerformanceAnalytics
package offers a very easy and flexible way of doing this.
Let's examine some of the arguments of this function:
chart.TimeSeries(R, period.areas, period.color)
R
is an xts
, time series, or zoo
object of asset returns, period.areas
are shaded areas specified by a start and end date in a vector of xts
date ranges like c("1926-10/1927-11")
, and period.color
draws the shaded region in whichever color is specified.
In this exercise, you will highlight a single period in a chart of the Citigroup time series in data
.
This exercise is part of the course
Visualizing Time Series Data in R
Exercise instructions
- Create an object called
period
containing the first three months of 2015 - Using the
chart.TimeSeries()
function, highlight the citigroup data values in theperiod
- Use
chart.TimeSeries()
again to redraw the same line chart but this time set the color of the highlighted period to"lightgrey"
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create period to hold the 3 months of 2015
# Highlight the first three months of 2015
# Highlight the first three months of 2015 in light grey