【问题标题】:Change name in xts loop plots在 xts 循环图中更改名称
【发布时间】:2020-08-19 12:09:33
【问题描述】:

我在四个 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 &lt;- 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 未显示在图中,即使设置了高边距。有人知道解决方案吗?

【问题讨论】:

    标签: r loops plot xts


    【解决方案1】:

    在情节中为 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]))
    }
    

    【讨论】:

    • 谢谢。这给了我"new.close", "new.close.1", "new.close.2", "new.close.3" 的名字,而不是我所追求的tickers &lt;- c("AAPL", "GE", "SPY", "WMT")
    • 美丽。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2020-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    相关资源
    最近更新 更多