日期 ggplot 问题
我最近开始使用 ggplot,我对 x 轴上日期时间的绘制有点困惑。
有问题的最小例子是这样的:
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")
这给出了一个错误:
> 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() 显示:
会话信息()
R版本2.12.2 (2011-02-25) 平台:i386-pc-mingw32/i386(32位)
语言环境: [1] LC_COLLATE=英语_美国.1252 LC_CTYPE=英语_美国.1252 > LC_MONETARY=英语_美国.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
附加基础包: [1] 网格 grDevices 数据集 样条线 图形统计 tcltk utils 方法库
其他附加包: [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 success_2.36-5
通过命名空间加载(且未附加): [1] cluster_1.13.3摘要_0.4.2lattice_0.19-17Matrix_0.999375-46>svMisc_0.9-61tools_2.12.2
和traceback()输出:
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 = ))
I've started using ggplot recently and I'm a bit confused with the plotting of datetime on the x-axis.
The smallest example with a problem is this:
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")
This gives ans error:
> qplot(DateTime, response, data=df)
Error in seq.int(r1$year, to$year, by) : 'from' must be finite
but this is working fine:
> qplot(DateTime, response, data=df[1:2,])
Why is my first plot failing?
As requested sessionInfo() shows:
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.1252attached base packages:
[1] grid grDevices datasets splines graphics stats tcltk utils methods baseother 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-5loaded 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
And traceback() output:
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 = ))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也有同样的问题。 Windows XP / US English / R 2.13.0
我最多可以绘制 476 条记录,但如果有 477 条或更多记录,则会弹出错误。
这有效:
这不起作用:
我不确定是否是记录数或第 477 条记录的性质导致了问题。
无论如何,解决方法似乎是指定主要和次要网格间隔,即
其中 p 是您的绘图。
I'm having the same problem. Windows XP / US English / R 2.13.0
I am able to plot up to 476 records, but the error pops up if I have 477 or more.
This works:
This does not:
I'm not sure if it is the number of records or the nature of the 477th record that is causing the problem.
In any case, a workaround seems to be to specify the major and minor grid intervals, ie
where p is your plot.
我也遇到了这个问题,但对我来说,自从我更新到 R 2.13.1 后,这个问题就解决了。 OP,你能检查一下并确认一下吗?
I had the issue as well, but for me it is solved since I have updated to R 2.13.1. OP, could you check and confirm this?
我在研究中最近的数据也遇到了同样的问题。
事实上,将
df[3,1]
更改为2011-04-30 08:49:50
(4 月 30 日)可以让它再次工作。此外,plot()
对此没有问题。所以 ggplot2 中似乎存在一些关于日期时间转换的错误。
编辑:我在我的 Mac 上重新测试了这个,没有问题,所以只在 Windows 上。下一步可能是检查这是否确实是区域设置问题。
I've experienced the same problem with recent data in my studies.
In fact, changing
df[3,1]
to2011-04-30 08:49:50
(April 30) makes it work again. Additionally,plot()
has no problem with this.So there seems to be some bug in ggplot2 regarding datetime conversions.
Edit: I've retested this on my mac and no problems there, so only on Windows. Next step is maybe to check if this is actually a locale issue.