tags:

views:

48

answers:

2

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?

+1  A: 

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):

data(AirPassengers)   # time series in months (supplied w/ default R install)
AP = AirPassengers
library(xts)
X = as.xts(AP)      # need xts object to pass to xts binning method
ndx = endpoints(X, on="years")    # binning method requires indices for desired bin freq
X_yr = period.apply(x=X, INDEX=ndx, FUN=sum)   # X_yr is the binned data
doug
I am not sure this solution were work for me; first because the data has a single column with date; and second, I do not think xts objects are compatible with ggplot2.
DrewConway
+1  A: 

I can reproduce your error as follows:

> 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

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 for Date objects in situations where it makes sense.

nullglob
I believe ggplot2 is throwing the error, as I am using dates as the x-axis scale.
DrewConway