Reputation: 1033
A newbie question. (ADDED NEW INFO)
I have a set of time stamped data that were collected randomly. I like to create a matrix of plots, but I could not create using either scatterplot or xyplot & time objects.
my data
dataset$Time #POSIX time objects (no set sampling period)
#i built POSIX time objects by dataset$Time<-strptime(tt, "%H:%M:%OS")
#the origial string was formated like this 12:12:12.234 (HH:MM:SS:msec)
dataset$d1, dataset$d2 #integers
dataset$d3 #factor with 10 levels
.
i can do these
plot( dataset$Time, dataset$d1)
scatterplot(dataset$d1 ~ dataset$d2 | dataset$d3, data=dataset)
xyplot(dataset$d1 ~ dataset$d2 | dataset$d3, data=dataset)
However, i can NOT do these (POSIX time object in x-axis)
scatterplot(dataset$d1 ~ dataset$Time | dataset$d3, data=dataset)
xyplot(dataset$d1 ~ dataset$Time | dataset$d3, data=dataset)
(NEW INFO)
Error in structure(.Internal(as.POSIXct(x, tz)), class = c("POSIXt", "POSIXct"), : invalid 'x' argument.
(NEW INFO) but this works (POSIX time object in y-axis)
xyplot(dataset$Time ~ dataset$d1 | dataset$d3, data=dataset)
related, but different question is hexbin. When time objects are added to hexbin, the plot from the hexbin does not show correct time format on the units.
bin<-hexbin(dataset$Time, dataset$d1)
plot(bin))
What should I do?
Thanks for looking into it!!
Upvotes: 0
Views: 2224
Reputation: 368261
Here is a working example. You may have gotten your data formats wrong -- one needs to be careful about the precise data formats.
First, a simple data frame:
R> X <- data.frame(pt=Sys.Date()+0:4, x1=100+cumsum(rnorm(5)),
+ x2=90+cumsum(rt(5,4)))
R> X
pt x1 x2
1 2010-06-22 98.73 90.33
2 2010-06-23 99.43 89.56
3 2010-06-24 98.85 86.95
4 2010-06-25 99.08 88.52
5 2010-06-26 100.30 94.08
R>
This is so-called wide form which lattice does not use. You need to transform it into long format. I use stack()
here, you can also use cast()
and melt()
from the reshape
package:
R> Y <- data.frame(pt=rep(X$pt,2), stack(X, select=c(x1,x2)))
R> Y
pt values ind
1 2010-06-22 98.73 x1
2 2010-06-23 99.43 x1
3 2010-06-24 98.85 x1
4 2010-06-25 99.08 x1
5 2010-06-26 100.30 x1
6 2010-06-22 90.33 x2
7 2010-06-23 89.56 x2
8 2010-06-24 86.95 x2
9 2010-06-25 88.52 x2
10 2010-06-26 94.08 x2
R>
Now the xyplot
call is simply:
R> xyplot(values ~ pt | ind, data=Y, panel=panel.lines)
and you can of course use much more complicated conditioning expressions.
Upvotes: 2
Reputation: 18628
For me it just works, so you probably has a time vector in a bad format. What do you get when you call class(dataset$Time)
? It should contain "POSIXct" for this to work.
On the other hand, you don't need to put dataset$
in formula if you supply data=dataset
.
Upvotes: 2
Reputation: 51640
Apparently R has some problem dealing with addiction of POSIX times... it gives me the error:
Error in
+.POSIXt
(x[floor(d)], x[ceiling(d)]) : binary '+' is not defined for "POSIXt" objects
just cast them to numeric and it should work. You can hide the corresponding axis and redraw it later with the correct dates
E.g.:
scatterplot(dataset$d1 ~ as.numeric(dataset$Time) | dataset$d3, data=dataset, xaxt="n")
axis(1, at=as.numeric(dataset$Time), labels=dataset$Time, cex.axis=0.5)
Upvotes: 2