使用 seq.Date 作为时间序列的 stat_bin 中的中断

发布于 2024-09-11 08:39:32 字数 554 浏览 2 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风铃鹿 2024-09-18 08:39:32

当我在其他情况下从 ggplot2 中得到该错误时,我已经能够在我用于中断的序列周围使用 as.numeric() 了。例如,

library(lubridate)
library(ggplot2)
library(scales)

somedates <- ymd(20130101) + ddays(runif(100, 0, 364)) #generate some fake event dates
somedatesformatted <- data.frame(Dates=as.Date(somedates)) #format them as data ggplot likes
monthbins <- as.numeric(seq(as.Date('2013-01-01'), as.Date('2014-01-01'), '1 month')) # generate breaks for binning event dates and wrap in as.numeric()

ggplot(somedatesformatted, aes(x=Dates)) + 
    stat_bin(breaks=monthbins) +
    ylab("Events per Month") +
    ylim(c(0,30)) +
    scale_x_date(breaks = '1 month',
             labels = date_format("%b"),
             limits = c(as.Date('2013-01-01'), as.Date('2013-12-31')))

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,

library(lubridate)
library(ggplot2)
library(scales)

somedates <- ymd(20130101) + ddays(runif(100, 0, 364)) #generate some fake event dates
somedatesformatted <- data.frame(Dates=as.Date(somedates)) #format them as data ggplot likes
monthbins <- as.numeric(seq(as.Date('2013-01-01'), as.Date('2014-01-01'), '1 month')) # generate breaks for binning event dates and wrap in as.numeric()

ggplot(somedatesformatted, aes(x=Dates)) + 
    stat_bin(breaks=monthbins) +
    ylab("Events per Month") +
    ylim(c(0,30)) +
    scale_x_date(breaks = '1 month',
             labels = date_format("%b"),
             limits = c(as.Date('2013-01-01'), as.Date('2013-12-31')))
吲‖鸣 2024-09-18 08:39:32

我可以按如下方式重现您的错误:

> 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

将两个日期加在一起没有意义,因为日期刻度上没有自然的“零”。然而,+ 运算符是在有意义的情况下为 Date 对象定义的。

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.

分开我的手 2024-09-18 08:39:32

在我看来,如果您首先转换数据,然后将其传递给绘图方法,您可以获得相同的结果。

例如(按年份分组的月份时间序列数据):

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文