R 比较绘图/导程中的不同时间窗口&;流动假期的滞后

R 比较绘图/导程中的不同时间窗口&;流动假期的滞后,r,ggplot2,data.table,R,Ggplot2,Data.table,我想比较情节中不同年份的某个事件(例如“复活节”),包括事件前后的几天。到目前为止,我只能比较事件本身: require('data.table') require('ggplot2') require('timeDate') #create some sample data a <- data.table(Date = seq(as.Date('2010-01-01'),as.Date('2012-12-31'), 'days'), Value = rnorm(

我想比较情节中不同年份的某个事件(例如“复活节”),包括事件前后的几天。到目前为止,我只能比较事件本身:

require('data.table')
require('ggplot2')
require('timeDate')

#create some sample data
a <- data.table(Date = seq(as.Date('2010-01-01'),as.Date('2012-12-31'), 'days'),
            Value = rnorm(1096))
a[as.Date(Easter(year(Date))) == Date,Easter := '1']

#create the plot
ggplot(a[!is.na(Easter),], aes(x=Easter, y=Value, group=as.factor(year(Date)), 
                              colour=as.factor(year(Date)))) + geom_point(size=5)
require('data.table'))
require('ggplot2')
require('timeDate')
#创建一些示例数据
a
#创建一些示例数据

a好吧,你把aes(x=Easter)
放在x轴上,这样你就得到了“Easter”列。我无法复制,因为我没有
Easter
函数。我尝试了
forecast::easter()
,但对您的数据类型无效。您好,Gregor,easter函数在timeDate包中,sry,我忘记了这个函数。使用复活节其实并不重要,它只是一个每年发生一次的随机事件,而不是固定的日期。你想实现什么?绘制每年的值?或者绘制连续复活节日期之间的数值??我想比较不同年份复活节前后的数值*复活节就像一个移动假期的例子,找到复活节前后的几周如下:
timeSequence(复活节(2008,-14),复活节(2008,+14))
a <- data.table(Date = seq(as.Date('2010-01-01'),as.Date('2012-12-31'), 'days'),
            Value = rnorm(1096))

a[as.Date(Easter(year(Date))) - 15 < Date & as.Date(Easter(year(Date))) + 15 > Date,
  Easter := as.integer(Date - as.Date(Easter(year(Date))))]

ggplot(a[!is.na(Easter),], aes(x=Easter, y=Value, group=as.factor(year(Date)), 
                              colour=as.factor(year(Date)))) +      
       geom_point(size=5) + geom_line()
#create some sample data
a <- data.table(Date = seq(as.Date('2010-01-01'),as.Date('2012-12-31'), 'days'),
            Value = rnorm(1096))

#mark the event itself
a[as.Date(Easter(year(Date))) == Date,Easter_event := 1]

#extract the indices of the event
event_list <- a[,.I[Easter_event==1]][!is.na(a[,.I[Easter_event==1]])]

#mark +-14 days around the event
for(ind in event_list){
   a[(ind - 14):(ind + 14), Easter := seq(-14,14)]
}