tags:

views:

53

answers:

2

I'm trying to use qplot() to plot a simple time series as one might do using plot(). The x variable is as.POSIXlt and the y is just some continuous measurement. Here is the code with some brief comments. Any help on why these data.frames behave differently would be very much appreciated. As you can see below, I can work around the problem, but I'm curious as to why is doesn't work as I would expect.

A few details:
platform: OS X 10.6.4
R version: R 2.11.0

Disclaimer: I realize that I could dig into the source code and figure this out myself. I've never used SO and thought that it might be a nice topic for this forum.

Disclaimer (2): I'm new to ggplot2

library(ggplot2)
ws.dat <- read.csv("~/path/to/filename.csv",header=F)
names(ws.dat) <- c("s","t","w")
ws.dat$new.t <- as.POSIXlt(ws.dat$t)
ws.dat[1:5,]
  ##       s                   t    w               new.t
  ## 1 29522 2005-07-02 00:00:00 5.00 2005-07-02 00:00:00
  ## 2 29522 2005-07-02 00:10:00 5.29 2005-07-02 00:10:00
  ## 3 29522 2005-07-02 00:20:00 5.48 2005-07-02 00:20:00
  ## 4 29522 2005-07-02 00:30:00 5.54 2005-07-02 00:30:00
  ## 5 29522 2005-07-02 00:40:00 5.49 2005-07-02 00:40:00


## the following works
plot(as.POSIXlt(ws.dat$t), ws.dat$w)

## doesn't work
qplot(as.POSIXlt(t), w, data = ws.dat)
  ## Error in if (length(range) == 1 || diff(range) == 0) { : 
  ## missing value where TRUE/FALSE needed

## doesn't work
ws.dat$new.t <- as.POSIXlt(ws.dat$t)
qplot(new.t, w, data = ws.dat)
  ## Same error as above

## Note - I could find a more elegant way of doing this; I'm just trying
##   to reproduce as fast as possible.
new.df <- data.frame(ws.dat$new.t, ws.dat$w)
new.df[1:5,]
  ##          ws.dat.new.t ws.dat.w
  ## 1 2005-07-02 00:00:00     5.00
  ## 2 2005-07-02 00:10:00     5.29
  ## 3 2005-07-02 00:20:00     5.48
  ## 4 2005-07-02 00:30:00     5.54
  ## 5 2005-07-02 00:40:00     5.49

## 'works as *I* would expect'; this is != 'works *as* expected' 
qplot(ws.dat.new.t, ws.dat.w, data = new.df)
+2  A: 

Use POSIXct - POSIXlt is not suitable for inclusion into data frames. When you use data.frame to create the variable it is automatically coerced to POSIXct.

hadley
+1  A: 

When it doubt, look at the class of the objects being passed! Thanks Hadley.

class(new.df$ws.dat.new.t)
## [1] "POSIXt"  "POSIXct"  <--- ct!!!!
class(as.POSIXlt(ws.dat$tt))
## [1] "POSIXt"  "POSIXlt"  <--- lt!!!!
rtelmore