簡単にするために、次の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.RollingCorrelation
パッケージから使用しPerformanceAnalytics
、次のようにチャートをプロットします。
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)
次のプロットを取得します。
両方のグラフに共通の凡例を取得し、それぞれの関係を 1 つの色で示してプロットの下部に配置する必要があります。の引数から凡例の指定を削除chart.RollingCorrelation
し、カスタマイズした凡例を使用して別のプロットを追加しようとしましたが、問題があります。プロット内のすべてのチャートが個別にプロットされているため、2 つの異なる関係が同じ色で表されていることがわかります。そのため、関数を適用する方法で何かを変更する必要があるようです。