2016-09-30 54 views
1

如何标注y轴,使用timeSeries::plot,用希腊字母?即将SB,SP等改为\ alpha,\ beta等,我知道我需要expression(),以某种方式。不过,我甚至无法访问标签(我通常使用ggplot2)。下面的代码。标签ylab在时间序列::情节,类型=“O”

plot

# 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对象LPPstats(时间序列对象)z

回答

1

[关于做好REVISION &编辑]

plot.type"multiple",我们不能直接定义ylabplot(ts.obj)S3方法)和plot(timeSeries.obj)S4方法)以colnames(obj)ylab,我不知道任何使用希腊字母作为colname的方法。 (在结构的不同,主要来源于S3S4的差值; colnames(timeSeries.obj)相当于[email protected];默认值是Series iTS.i)。

我们可以在ylab使用arugument步骤,panel(就是了function,默认为lines)。它用于for(i in 1:ncol(data))。我不能给panel.function一个合适的"i"(我想它可以以某种方式,但我没有想到),所以我得到"i"使用哪个col数据匹配。

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) 

enter image description here

当然你也可以使用从原来的(主要是一样取得了新的功能archieve吧原版的) 。我只显示了修改后的点。

改性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)

# made from `.plot.timeSeries` 
my.plot.timeSeries <- function(~~~, my.ylab = NULL) { 
    : 
    my.plotTimeSeries(~~~, my.ylab = my.ylab) 
} 

# made from `timeSeries:::.plotTimeSeries` 
my.plotTimeSeries <- function(~~~, my.ylab) { 
    : 
    nm <- my.ylab  # before: nm <- colnames(x) 
    : 
} 

#use 
my.plot.timeSeries(LPP, my.ylab = expression(alpha, beta, gamma, delta), type="o")