Eric Fail
Eric Fail

Reputation: 7928

label ylab in timeSeries::plot, type = 'o'

How do I label the y-axis, using timeSeries::plot, with Greek letters? i.e. change SB, SP, etc. to \alpha, \beta etc., I'm am aware I need expression(), in some way. However I can't even get to the labels (I normally use ggplot2). Code below.

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")      

It have been pointed out that the object structure, obtained with str(), is quite particular in LPP compared to say this object z

z <- ts(matrix(rnorm(300), 100, 3), start = c(1961, 1), frequency = 12)
plot(z)

If any one has an answer to both or any I would appreciate it. I realize I can convert the data and plot it with ggplot2, I have seen that here on SO, but I am interested in doing in directly on the timeSeries object LPP and the stats (time-series object) z

Upvotes: 1

Views: 471

Answers (1)

cuttlefish44
cuttlefish44

Reputation: 6796

[ REVISION & Edited ]

When plot.type is "multiple", we can't define ylab directly. Both plot(ts.obj) (S3 method) and plot(timeSeries.obj) (S4 method) take colnames(obj) as ylab, and I don't know any methods of using Greek letters as colname. (The difference in structure mainly comes from the difference of S3 and S4; colnames(timeSeries.obj) is equivalent to timeSeries.obj@units; the defaults is Series i and TS.i).

We can step in ylab using the arugument, panel (It wants a function and the default is lines). It is used in for(i in 1:ncol(data)). I couldn't give panel.function a suitable "i" (I guess it can in some way, but I didn't think up), so I got "i" using which col the data matches.

for 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)

for 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

Of course you can archieve it using new functions made from the original (mostly the same as the original). I showed only the modified points.

modified plot(ts.obj) (made from 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")

modified 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")

Upvotes: 1

Related Questions