[R-SIG-Finance] working levelplot with zoo - surface plot of multivariate time series
Christian Gunning
icos.atropa at gmail.com
Wed Jun 3 06:17:12 CEST 2009
On Tue, Jun 2, 2009 at 3:00 AM,
<r-sig-finance-request at stat.math.ethz.ch> wrote:
>
> Now I want to get "surface plot" wherein x-axis is time, y-axis represents
> 1,2,3,4, and z-axis is for values of "dat2". Is there any R drawing device
> to do that?
>
I just ran into this independently. Is a method of levelplot for zoo
objects on anyone's horizon? Nontrivial? I got the following to
work:
start = as.Date("01/01/05", format="%m/%d/%y")
end = as.Date("12/31/05", format="%m/%d/%y")
dates = seq(start, end, by=1)
dat = matrix(rnorm(length(dates)*4), length(dates))
dat1 = t(apply(dat, 1, function(x) x+t(c(100, 110, 120, 130))))
library(zoo)
dat2 = zoo(dat1, dates); head(dat2)
### With Date index This works, but it isn't pretty:
mylab=lattice:::formattedTicksAndLabels.Date(index(dat2))
levelplot(coredata(dat2),
scales=list(x=list(rot=90,at=mylab$at-mylab$num.limit[1],
labels=mylab$labels)), aspect='fill')
This only works for dates, where the index vector increment and the
numeric representation of the Date increment are both 1. You can
subtract out "mylab$num.limit" to get them to align. This doesn't
work for POSIXct, where the numeric representation of the POSIXct
increment is in seconds (unless the index vector is by second). Here,
I extract the numeric representation of dt from the index vector, and
divide the "at" vector by dt to align it with the labels. It works,
just really messy.
### With POSIXct index
start =as.Date("01/01/05", format="%m/%d/%y")
end = as.Date("12/31/05", format="%m/%d/%y")
dates = as.POSIXct(seq(start, end, by=1)) ####changed
dat = matrix(rnorm(length(dates)*4), length(dates))
dat1 = t(apply(dat, 1, function(x) x+t(c(100, 110, 120, 130))))
mylab=lattice:::formattedTicksAndLabels.POSIXct(index(dat2))
mylab.at = (mylab$at-mylab$num.limit[1])/(as.numeric(dates[2])-as.numeric(dates[1]))
levelplot(coredata(dat2), scales=list(x=list(rot=90,at=mylab.at,
labels=mylab$labels)), aspect='fill')
###################
hope this helps,
christian
--
Far better an approximate answer to the right question, which is often
vague, than the exact answer to the wrong question, which can always
be made precise -- j.w. tukey
More information about the R-SIG-Finance
mailing list