2011-04-07 98 views
1

我已經開始使用ggplot最近,我有點困惑與日期時間的X軸繪圖。日期ggplot問題

有問題的最小的例子是這樣的:

df = data.frame(DateTime=c("2011-03-29 22:44:01", "2011-03-30 00:08:36", "2011-04-07 08:49:50"), response=c(1,2,3)) 

df$DateTime = as.POSIXct(df$DateTime, format="%Y-%m-%d %H:%M:%S") 

這給ANS錯誤:

> qplot(DateTime, response, data=df) 
Error in seq.int(r1$year, to$year, by) : 'from' must be finite 

,但這是工作的罰款:

> qplot(DateTime, response, data=df[1:2,]) 

爲什麼是我第一次陰謀失敗?

按照要求sessionInfo()顯示:

sessionInfo()

R version 2.12.2 (2011-02-25) Platform: i386-pc-mingw32/i386 (32-bit)

locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 >LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages: [1] grid grDevices datasets splines graphics stats tcltk utils methods base

other attached packages: [1] car_2.0-9 nnet_7.3-1 MASS_7.3-11 RColorBrewer_1.0-2 doBy_4.3.0 ggplot2_0.8.9 proto_0.3-9.1 reshape_0.8.4
[9] plyr_1.4.1 svSocket_0.9-51 TinnR_1.0.3 R2HTML_2.2 >Hmisc_3.8-3 survival_2.36-5

loaded via a namespace (and not attached): [1] cluster_1.13.3 digest_0.4.2 lattice_0.19-17 Matrix_0.999375-46 >svMisc_0.9-61 tools_2.12.2

而回溯()輸出:

 

traceback() 
20: seq.POSIXt(floor_time(range[1], time), ceiling_time(range[2], 
     time), by = time) 
19: fullseq_time(d, .$break_points()[2]) 
18: get(x, envir = this, inherits = inh)(this, ...) 
17: scales$x$output_breaks() 
16: inherits(x, "factor") 
15: is.factor(x) 
14: rescale(data, 0:1, range, clip = clip) 
13: get(x, envir = this, inherits = inh)(this, ...) 
12: .$rescale_var(scales$x$output_breaks(), x.range, TRUE) 
11: get(x, envir = this, inherits = inh)(this, ...) 
10: coord$compute_ranges(scales) 
9: FUN(1L[[1L]], ...) 
8: lapply(seq_along(data), function(i) { 
     layer), 
     scales = , mapping = list(x = DateTime, y = response), 
     options = list(labels = list(x = "DateTime", y = "response")), 
     coordinates = , facet = , plot_env =)) 
1: print(list(data = list(DateTime = c(1301431441, 1301436516, 1302158990 
    ), response = c(1, 2, 3)), layers = list(), scales = , 
     mapping = list(x = DateTime, y = response), options = list(
      labels = list(x = "DateTime", y = "response")), coordinates = , 
     facet = , plot_env =)) 

+0

它適用於我。你可以發佈你的'sessionInfo()'嗎? – 2011-04-07 16:07:24

+0

它也適用於我。我在Mac OS 10.5上使用R 2.12.2。如Iselzer所示,發佈sessionInfo()將有助於 – Ramnath 2011-04-07 16:59:21

+0

如果他正在使用'ggplot'而不是'ggplot2',那麼他就會過時。 – 2011-04-07 20:05:27

回答

0

我已經經歷了同樣的問題,在我的研究中最新的數據。

事實上,將df[3,1]更改爲2011-04-30 08:49:50(4月30日)將使其重新開始工作。此外,plot()對此沒有任何問題。

因此,ggplot2中的日期時間轉換似乎存在一些錯誤。

編輯:我在我的Mac上重新測試了這個,沒有問題,所以只在Windows上。下一步可能是檢查這是否是一個本地化問題。

1

我遇到同樣的問題。 Windows XP/US English/R 2.13.0

我可以繪製多達476條記錄,但如果我有477個或更多記錄,則彈出錯誤。

這工作:

data=subset(Pressures,DaysAgo<7)[1:476,]) 

這不:

data=subset(Pressures,DaysAgo<7)[1:477,]) 

我不知道,如果它是的記錄數或者是造成問題的第477次記錄的性質。

在任何情況下,解決方法似乎是指定主要和次要的網格間距,即

p <- p+scale_x_datetime(major="1 day",minor="6 hours") 

其中p是你的陰謀。

0

我也有這個問題,但對我來說,它已經解決了,因爲我已經更新到R 2.13.1。 OP,你可以檢查並確認這一點嗎?

R version 2.13.1 (2011-07-08) 
Platform: x86_64-pc-mingw32/x64 (64-bit) 

locale: 
[1] C 

attached base packages: 
[1] grid  stats  graphics grDevices utils  datasets methods base  

other attached packages: 
[1] lubridate_0.2.5 ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 reshape2_1.1   xtable_1.5-6 plyr_1.5.2  
[8] zoo_1.6-5  

loaded via a namespace (and not attached): 
[1] digest_0.5.0 lattice_0.19-30 stringr_0.5  tools_2.13.1