2016-09-30 28 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")