zoo 包支持具有灵活时间索引的时间序列,例如,包括Date。该软件包还带来了一个fortify() 方法,可用于ggplot2 图形。还提供了autoplot() 的便捷方法,请参阅?autoplot.zoo 以选择具有不同布局的工作示例。
对于一个具有 18 个时间序列的示例,索引为 3 x 6 布局中的 Date,我使用来自包 fxregime 的数据集 FXRatesCHF 的子集。这提供了相对于瑞士法郎 (CHF) 的不同货币的汇率。
library("zoo")
data("FXRatesCHF", package = "fxregime")
FX <- window(FXRatesCHF[, c(1:4, 6:19)], start = as.Date("2000-01-01"))
然后ggplot()可以应用到fortify()方法的输出:
library("ggplot2")
ggplot(aes(x = Index, y = Value), data = fortify(FX, melt = TRUE)) +
geom_line(color = "darkred") +
xlab("Time") + ylab("FX") +
theme_bw() +
facet_wrap(~ Series, scales = "free_y", ncol = 6)
同样的布局也可以通过基本图形轻松创建。只有面板标题在 y 轴上,而不是在灰色阴影主标题中:
plot(FX, col = "darkred", xlab = "Time", nc = 6,
panel = function(...) { grid(col = "lightgray"); lines(...) })
最后,lattice 版本可以由
创建
library("lattice")
trellis.par.set(theme = standard.theme(color = FALSE))
xyplot(FX, col = "darkred", xlab = "Time", layout = c(6, 3),
panel = function(...) { panel.grid(col = "lightgray"); panel.lines(...) })