views:

105

answers:

3

Hi!

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!!

+1  A: 

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)
nico
Hi nico! as.numeric() and as.POSIXct() let me use xyplot but NOT with scatterplot(). have you tried with scatterplot() from car package?
any alternative way to represent time from time stamps?
yes, I've tried with scatterplot, as in the example I wrote in my answer... may be an error in how you create/import the data? can you maybe provide a sample of your data?
nico
+2  A: 

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.

mbq
mbq, you're right. with help from nico below, i was able to use xyplot by casting either by as.numeric() or by as.POSIXct(). However, scatterplot was a no-go so far. Thank you!!
+1  A: 

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.

Dirk Eddelbuettel
Hi Dirk! myabe, it is because of the difference in localization, but my time object is shown as this "2010-06-21 17:45:42" What are x1, x2, and ind? Thank you!
I *created the example data on the spot* which is why you get different time values. The two columns are cumulative sums of random number from the normal and t distributions.
Dirk Eddelbuettel
the problem is not the data format, but time format. it seems that R has bit of issues with POSIXct vs POSIXlt. Thanks!
That's a FAQ. You want `POSIXct` (== a single double) and not `POSIXlt` (== a list of nine elements) in your `data.frame` -- so call `as.POSIXct()` on the columns.
Dirk Eddelbuettel
hexbinplot() giving me errors with POSIXct "Error in '/.difftime'(diff(ybonds), diff(.ylim) : second argument of / cannot be a "difftime" object" ----- scatterplot() giving me "Error in '+.Date'(x[floor(d)], x[ceiling(d)]) : binary + is not defined for Date objects --- any suggestion to represent time in a format i would not get these errors? Thank you!!
Nobody claimed `hexbin()` would with DateTime classes, that is only you assuming it does. I showed you how to use `xyplot()` with DateTime data, this does in no way imply `hexbin()` would. So once again, if it doesn't work, transform your data. `as.numeric()` on a `POSIXct` gives you a nice and clean floating point that `hexbin()` will have no trouble with.
Dirk Eddelbuettel