我试图使用stat_bin函数中的几个月观察数年的时间序列数据ggplot2.代码如下所示:
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)
Run Code Online (Sandbox Code Playgroud)
但是,当我运行print时,我收到以下错误:
Error in `+.Date`(left, right) : binary + is not defined for Date objects
Run Code Online (Sandbox Code Playgroud)
如果我正确读取这个,则没有为Date对象定义加号运算符,因此无法执行这种类型的分箱操作?
我可以重现您的错误,如下所示:
> break.dates <- seq.Date(from=as.Date('2001-01-01'),
to=as.Date('2001-04-01'),
by="month")
> break.dates
[1] "2001-01-01" "2001-02-01" "2001-03-01" "2001-04-01"
## add an offset, which increases the dates by a day
> break.dates + 1
[1] "2001-01-02" "2001-02-02" "2001-03-02" "2001-04-02"
## try to add two date objects together - this does not make sense!
> break.dates + break.dates
Error in `+.Date`(break.dates, break.dates) :
binary + is not defined for Date objects
Run Code Online (Sandbox Code Playgroud)
将两个日期加在一起是没有意义的,因为日期刻度上没有自然的“零”。然而,+运算符是Date在有意义的情况下为对象定义的。