在时间序列中高亮特定区间
为了在时间序列中突出某一特定区间,您可以在图中用不同的背景颜色显示该区间。PerformanceAnalytics 包中的 chart.TimeSeries() 函数提供了非常简便且灵活的方式来实现这一点。
让我们看看该函数的一些参数:
chart.TimeSeries(R, period.areas, period.color)
R 是包含资产收益的 xts、时间序列或 zoo 对象;period.areas 是用 xts 日期范围向量(如 c("1926-10/1927-11"))指定的着色区间的起止日期;period.color 用指定的颜色填充着色区域。
在本练习中,您将对 data 中花旗集团(Citigroup)的时间序列图表高亮显示一个区间。
本练习是课程的一部分
在 R 中可视化时间序列数据
练习说明
- 创建名为
period的对象,包含 2015 年前 3 个月。 - 使用
chart.TimeSeries()函数,高亮显示period中的花旗集团数据值。 - 再次使用
chart.TimeSeries()重绘相同的折线图,但这一次将高亮区间的颜色设置为"lightgrey"。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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