2

为简单起见,假设我有以下zoo对象:

x.ts<- structure(c(103.7, 103.2, 103.1, 105.4, 102.1, 103.5, 103.1, 
102.6, 102.2, 104.6, -2.1, -1, -3, 2, -1, 1, -1, -1, -1, 0, -25, 
-25, -25, -25, -25, -25, -21, -21, -20, -20), .Dim = c(10L, 3L
), .Dimnames = list(c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10"), c("a", "b", "c")), index = structure(c(1985, 1985.08333333333, 
1985.16666666667, 1985.25, 1985.33333333333, 1985.41666666667, 
1985.5, 1985.58333333333, 1985.66666666667, 1985.75), class = "yearmon"), class = "zoo")

我有兴趣绘制对象中每对时间序列之间的滚动相关性。我chart.RollingCorrelationPerformanceAnalytics包中使用并绘制图表如下:

par.corr<-par(mfrow=c(1,2), oma = c(1, 1, 1, 1), mar = c(2, 2, 2, 2))
chart.RollingCorrelation(x.ts[, 1, drop=FALSE],
                         x.ts[, 1:3, drop=FALSE],
                         colorset=rich8equal,legend.loc = "right",
                         width=3, main = "a to b and c")
chart.RollingCorrelation(x.ts[, 2, drop=FALSE],
                         x.ts[, c(1,3), drop=FALSE],
                         colorset=rich8equal,legend.loc = "right",
                         width=3, main = "b to a and c")
mtext("Rolling 3 Month Correlation", side=3, line=-0.3, outer=TRUE, cex=1.2)
par(par.corr)

我得到以下情节:

滚动相关

我需要为两个图表获取一个共同的图例,用一种颜色描述每种关系,并将其放在图的底部。我试图从chart.RollingCorrelation的参数中删除图例规范,并使用我的自定义图例添加另一个图,但存在问题。因为图中的每个图表都是单独绘制的,所以您会发现两种不同的关系用相同的颜色表示。所以看来我需要改变我应用函数的方式。

4

0 回答 0