在xts循环图中更改名称

问题描述 投票:0回答:1

我在四个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"
    )))

PLot of four cumulative returns

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

[另外:即使设置了高边距,ylab和xlab也不会显示在图中。有人知道解决方案吗?

r loops plot xts
1个回答
0
投票

地块中主名称的调用名称功能

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 = names(basket[, i])))
}
© www.soinside.com 2019 - 2024. All rights reserved.