如何使用timeSeries::plot
用希腊字母标记y轴?即将SB,SP等更改为\ alpha,\ beta等,我知道我需要expression()
,以某种方式。但是我甚至无法访问标签(我通常使用ggplot2)。代码如下。
# install.packages("xtable", dependencies = TRUE)
library("timeSeries")
## Load Swiss Pension Fund Benchmark Data -
LPP <- LPP2005REC[1:12, 1:4]
colnames(LPP) <- abbreviate(colnames(LPP), 2)
finCenter(LPP) <- "GMT"
timeSeries::plot(LPP, type = "o")
有人指出,使用str()
获得的对象结构在LPP
中非常特别,而不是说这个对象z
z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1), frequency = 12)
plot(z)
如果任何人对这两者都有答案,或者我会很感激。我意识到我可以转换数据并用ggplot2绘制它,我在SO上看到了这一点,但我有兴趣直接在timeSeries
对象LPP
和stats
(时间序列对象)z
答案 0 :(得分:1)
[REVISION&amp;已编辑]
当plot.type
为"multiple"
时,我们无法直接定义ylab
。 plot(ts.obj)
(S3
方法)和plot(timeSeries.obj)
(S4
方法)都将colnames(obj)
作为ylab
,我不知道任何方法使用希腊字母作为colname。 (结构上的差异主要来自S3
和S4
的差异; colnames(timeSeries.obj)
相当于timeSeries.obj@units
;默认值为Series i
和{{1} })。
我们可以使用TS.i
进入ylab
(它需要panel
,默认为function
)。它在lines
中使用。我无法给for(i in 1:ncol(data))
一个合适的panel.function
(我猜它可以某种方式,但我没有想到),所以我得到了"i"
使用哪个col匹配数据。
"i"
的
timeSeries
ylabs <- expression(alpha, beta, gamma, delta)
row1 <- LPP[1,]
timeSeries.panel.f <- function(x, y, ...) {
lines(x, y, ...)
mtext(ylabs[which(row1 %in% y[1])], 2, line = 3)
}
plot(LPP, panel = timeSeries.panel.f, type = "o", ann = F)
title("Title")
mtext("Time", 1, line = 3)
## If you aren't so concerned about warnings, here is more general.
## (Many functions read `...` and they return warnings).
timeSeries.panel.f2 <- function(x, y, ..., ylabs = ylabs, row1 = row1) {
lines(x, y, ...)
mtext(ylabs[which(row1 %in% y[1])], 2, line = 3)
}
plot(LPP, panel = timeSeries.panel.f2, type = "o", ann = F,
ylabs = expression(alpha, beta, gamma, delta), row1 = LPP[1,])
title("Title")
mtext("Time", 1, line = 3)
当然,您可以使用原始制作的新功能(大多与原始功能相同)来实现它。我只显示了修改过的点。 已修改 已修改ts
ylabs <- expression(alpha, beta, gamma)
row1 <- z[1,]
ts.panel.f <- function(y, ...) {
lines(y, ...)
mtext(ylabs[which(row1 %in% y[1])], 2, line = 3)
}
plot(z, panel = ts.panel.f, ann = F)
title("Title")
mtext("Time", 1, line = 3)
(由plot(ts.obj)
制作)plot.ts
my.plot.ts <- function(~~~, my.ylab = NULL) {
:
nm <- my.ylab # before: nm <- colnames(x)
:
}
# use
my.plot.ts(z, my.ylab = expression(alpha, beta, gamma), type = "o")
plot(timeSeries.obj)