使用 seq.Date 作为时间序列的 stat_bin 中的中断
我尝试使用 ggplot2 中的 stat_bin 函数按月对几年观察的时间序列数据进行分类。代码如下所示:
month.breaks<-seq.Date(from=min(afg$DateOccurred),to=max(afg$DateOccurred),by="month") # All months, for breaks
report.region<-ggplot(afg,aes(x=DateOccurred))+stat_bin(aes(y=..density..,fill=Type),breaks=month.breaks)+facet_wrap(~Region)
print(report.region)
但是,当我运行 print 时,出现以下错误:
Error in `+.Date`(left, right) : binary + is not defined for Date objects
如果我正确地阅读了此内容,则没有为 Date
对象定义加号运算符,因此无法执行这种类型的分箱?
I am attempting to bin time-series data from several years of observation by month using the stat_bin
function in ggplot2
. The code looks like this:
month.breaks<-seq.Date(from=min(afg$DateOccurred),to=max(afg$DateOccurred),by="month") # All months, for breaks
report.region<-ggplot(afg,aes(x=DateOccurred))+stat_bin(aes(y=..density..,fill=Type),breaks=month.breaks)+facet_wrap(~Region)
print(report.region)
When I run print, however, I get the following error:
Error in `+.Date`(left, right) : binary + is not defined for Date objects
If I am reading this correctly, the plus operator is not defined for Date
objects and thus it is not possible to perform this type of binning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我在其他情况下从 ggplot2 中得到该错误时,我已经能够在我用于中断的序列周围使用 as.numeric() 了。例如,
When I've gotten that error from ggplot2 in other circumstances, I've been able to get away with using as.numeric() around the sequence I'm using for breaks. For example,
我可以按如下方式重现您的错误:
将两个日期加在一起没有意义,因为日期刻度上没有自然的“零”。然而,
+
运算符是在有意义的情况下为Date
对象定义的。I can reproduce your error as follows:
It does not make sense to add two dates together, since there is no natural "zero" on the date scale. However the
+
operator is defined forDate
objects in situations where it makes sense.在我看来,如果您首先转换数据,然后将其传递给绘图方法,您可以获得相同的结果。
例如(按年份分组的月份时间序列数据):
It seems to me you can get the same result if you just transform the data first, then pass it to the plot method.
For instance (time series data in months binned by year):