首页 > 解决方案 > 在 xts 循环图中更改名称

问题描述

我在四个 xts 图上有一个循环:

basket <- cbind(AAPLG, GEG, SPYG, WMTG)
tickers <- c("AAPL", "GE", "SPY", "WMT")
par(mar = c(3, 3, 3, 3))
par(mfrow=c(2,2))
for (i in 1:4){
print(plot.xts(x = basket[, i], xlab = "Time", ylab = "Cumulative Return",
     , major.ticks= "years",
     minor.ticks = FALSE, col = "black"))
}

我已经从 SO 帖子中删除了每个图顶部的样本空间:Changes in plotting an XTS object 但是,有没有办法将每个图的标题更改为上面的股票代码?所以在运行时循环 basket[, i]将被替换为tickers <- c("AAPL", "GE", "SPY", "WMT")

数据样本:

structure(c(1, 1.01463414634146, 0.926829268292683, 0.970731707317073, 
0.953658536585366, 1, 0.998263888888889, 1.01159722222222, 1.05076388888889, 
1.05034722222222, 1, 1.00178890876565, 0.985688729874776, 1.04293381037567, 
1.04651162790698, 1, 0.976675478152698, 0.990359197636448, 1.06515316436013, 
1.04571606282071), class = c("xts", "zoo"), index = structure(c(946944000, 
947030400, 947116800, 947203200, 947462400), tzone = "UTC", tclass = "Date"), .Dim = 5:4, .Dimnames = list(
    NULL, c("new.close", "new.close.1", "new.close.2", "new.close.3"
    )))

四个累积收益的图

这是我到目前为止的情节。都称为篮子 [ ,i] 信息量不是很大。

另外: ylab 和 xlab 未显示在图中,即使设置了高边距。有谁知道解决方案?

标签: rloopsplotxts

解决方案


在图中为 main 调用ticker对象函数

for (i in 1:4){
  print(plot.xts(x = basket[, i], xlab = "Time", ylab = "Cumulative Return",
                 major.ticks= "years", minor.ticks = FALSE, 
                 col = "black", main = tickers[i]))
}

推荐阅读