qplot()中的行为可能不一致吗? [英] Possibly inconsistent behavior in qplot()?
问题描述
我正在尝试使用qplot()绘制一个简单的时间序列,就像使用plot()所做的那样. x变量为as.POSIXlt,而y只是一些连续测量.这是带有一些简短注释的代码.对于为什么这些data.frame行为不同的任何帮助,将不胜感激.正如您在下面看到的那样,我可以解决此问题,但是我很好奇为什么它不能按我预期的那样工作.
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.
一些细节:
平台:OS X 10.6.4
R版本:R 2.11.0
A few details:
platform: OS X 10.6.4
R version: R 2.11.0
免责声明:我意识到我可以深入研究源代码并自己解决这个问题.我从未使用过SO,因此认为这对本论坛来说可能是一个不错的话题.
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.
免责声明(2):我是ggplot2的新手
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)
推荐答案
使用POSIXct
-POSIXlt
不适合包含在数据帧中.当您使用data.frame
创建变量时,它会自动强制为POSIXct
.
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
.
这篇关于qplot()中的行为可能不一致吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!