在時間序列中強調特定期間
若要在時間序列中強調某個特定期間,你可以在圖上以不同的背景色標示該區段。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中的 citigroup 資料值。 - 再次使用
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