R循环xts图
R loop xts plots
我被困在一个可能很简单的问题上:在 xts 对象上循环。
我想为篮子中的元素绘制四个不同的图:basket <- cbind(AAPLG, GEG, SPYG, WMTG)
> head(basket)
new.close new.close.1 new.close.2 new.close.3
2000-01-04 1.0000000 1.0000000 1.0000000 1.0000000
2000-01-05 1.0146341 0.9982639 1.0017889 0.9766755
2000-01-06 0.9268293 1.0115972 0.9856887 0.9903592
2000-01-07 0.9707317 1.0507639 1.0429338 1.0651532
2000-01-10 0.9536585 1.0503472 1.0465116 1.0457161
2000-01-11 0.9048780 1.0520833 1.0339893 1.0301664
到目前为止这是我的想法,因为我不能简单地输入 i 作为列名:
tickers <- c("AAPLG", "GEG", "SPYG", "WMTG")
par(mfrow=c(2,2))
for (i in 1:4){
print(plot(x = basket[, [i]], xlab = "Time", ylab = "Cumulative Return",
main = "Cumulative Returns", ylim = c(0.0, 3.5), major.ticks= "years",
minor.ticks = FALSE, col = "red"))
}
这是我在 运行 脚本时得到的错误:
Error: unexpected ',' in " main = "Cumulative Returns","
> minor.ticks = FALSE, col = "red"))
Error: unexpected ',' in " minor.ticks = FALSE,"
> }
Error: unexpected '}' in "}"
非常感谢任何帮助。
如前所述,删除 i
:
周围的方括号
par(mfrow=c(2,2))
for (i in 1:4){
print(plot(x = basket[, i], xlab = "Time", ylab = "Cumulative Return",
main = "Cumulative Returns", ylim = c(0.0, 3.5), major.ticks= "years",
minor.ticks = FALSE, col = "red"))
}
但更好的是,在构建 xts
object 或 re-name 您的 xts
object 中为 cbind
分配名称,就像任何数据框一样,然后遍历列引用和标题的名称:
情节
# PASS NAMES WITH cbind
basket <- cbind(AAPLG=APPLG, GEG=GEG, SPYG=SPYG, WMTG=WMTG)
# RENAME AFTER cbind
# basket <- cbind(AAPLG, GEG, SPYG, WMTG)
# colnames(basket) <- c("AAPLG", "GEG", "SPYG", "WMTG")
par(mfrow=c(2,2))
sapply(names(basket), function(col)
print(plot(x = basket[, col], xlab = "Time", ylab = "Cumulative Return", data = basket,
main = paste(col, "Cumulative Returns"), ylim = c(0.0, 3.5),
major.ticks= "years", minor.ticks = FALSE, col = "red"))
)
我被困在一个可能很简单的问题上:在 xts 对象上循环。
我想为篮子中的元素绘制四个不同的图:basket <- cbind(AAPLG, GEG, SPYG, WMTG)
> head(basket)
new.close new.close.1 new.close.2 new.close.3
2000-01-04 1.0000000 1.0000000 1.0000000 1.0000000
2000-01-05 1.0146341 0.9982639 1.0017889 0.9766755
2000-01-06 0.9268293 1.0115972 0.9856887 0.9903592
2000-01-07 0.9707317 1.0507639 1.0429338 1.0651532
2000-01-10 0.9536585 1.0503472 1.0465116 1.0457161
2000-01-11 0.9048780 1.0520833 1.0339893 1.0301664
到目前为止这是我的想法,因为我不能简单地输入 i 作为列名:
tickers <- c("AAPLG", "GEG", "SPYG", "WMTG")
par(mfrow=c(2,2))
for (i in 1:4){
print(plot(x = basket[, [i]], xlab = "Time", ylab = "Cumulative Return",
main = "Cumulative Returns", ylim = c(0.0, 3.5), major.ticks= "years",
minor.ticks = FALSE, col = "red"))
}
这是我在 运行 脚本时得到的错误:
Error: unexpected ',' in " main = "Cumulative Returns","
> minor.ticks = FALSE, col = "red"))
Error: unexpected ',' in " minor.ticks = FALSE,"
> }
Error: unexpected '}' in "}"
非常感谢任何帮助。
如前所述,删除 i
:
par(mfrow=c(2,2))
for (i in 1:4){
print(plot(x = basket[, i], xlab = "Time", ylab = "Cumulative Return",
main = "Cumulative Returns", ylim = c(0.0, 3.5), major.ticks= "years",
minor.ticks = FALSE, col = "red"))
}
但更好的是,在构建 xts
object 或 re-name 您的 xts
object 中为 cbind
分配名称,就像任何数据框一样,然后遍历列引用和标题的名称:
情节
# PASS NAMES WITH cbind
basket <- cbind(AAPLG=APPLG, GEG=GEG, SPYG=SPYG, WMTG=WMTG)
# RENAME AFTER cbind
# basket <- cbind(AAPLG, GEG, SPYG, WMTG)
# colnames(basket) <- c("AAPLG", "GEG", "SPYG", "WMTG")
par(mfrow=c(2,2))
sapply(names(basket), function(col)
print(plot(x = basket[, col], xlab = "Time", ylab = "Cumulative Return", data = basket,
main = paste(col, "Cumulative Returns"), ylim = c(0.0, 3.5),
major.ticks= "years", minor.ticks = FALSE, col = "red"))
)