【问题标题】:Split time series data into time intervals (say an hour) and then plot the count将时间序列数据拆分为时间间隔(比如一小时),然后绘制计数
【发布时间】:2012-11-18 21:33:25
【问题描述】:

我只有一个包含一列时间序列的数据文件:

'2012-02-01 17:42:44'
'2012-02-01 17:42:44'
'2012-02-01 17:42:44'

... 我想将数据拆分,以便在小时数的顶部进行计数。说:

'2012-02-01 17:00:00'  20   
'2012-02-01 18:00:00'  30  

“20”和“30”表示该期间的时间序列条目数。我希望能够绘制时间与“计数”的关系。我怎样才能用 R 做到这一点?

这是我当前的折线图。

library(ggplot2)

req <- read.table("times1.dat")
summary(req)

da <- req$V2
db <- req$V1

time <- as.POSIXct(db)

png('time_data_errs.png', width=800, height=600)
gg <- qplot(time, da) + geom_line()

print(gg)
dev.off()

【问题讨论】:

    标签: r datetime ggplot2


    【解决方案1】:

    听起来您想使用cut 来计算一个小时内出现了多少个值。

    如果您能提供一些示例数据,通常会很有帮助。这里有一些:

    set.seed(1) # So you can get the same numbers as I do
    MyDates <- ISOdatetime(2012, 1, 1, 0, 0, 0, tz = "GMT") + sample(1:27000, 500)
    head(MyDates)
    # [1] "2012-01-01 01:59:29 GMT" "2012-01-01 02:47:27 GMT" "2012-01-01 04:17:46 GMT"
    # [4] "2012-01-01 06:48:39 GMT" "2012-01-01 01:30:45 GMT" "2012-01-01 06:44:13 GMT"
    

    您可以使用tablecut(带有参数breaks="hour"(有关详细信息,请参阅?cut.Date))来查找每小时的频率。

    MyDatesTable <- table(cut(MyDates, breaks="hour"))
    MyDatesTable
    # 
    # 2012-01-01 00:00:00 2012-01-01 01:00:00 2012-01-01 02:00:00 2012-01-01 03:00:00 
    #                  59                  73                  74                  83 
    # 2012-01-01 04:00:00 2012-01-01 05:00:00 2012-01-01 06:00:00 2012-01-01 07:00:00 
    #                  52                  62                  64                  33 
    # Or a data.frame if you prefer
    data.frame(MyDatesTable)
    #                  Var1 Freq
    # 1 2012-01-01 00:00:00   59
    # 2 2012-01-01 01:00:00   73
    # 3 2012-01-01 02:00:00   74
    # 4 2012-01-01 03:00:00   83
    # 5 2012-01-01 04:00:00   52
    # 6 2012-01-01 05:00:00   62
    # 7 2012-01-01 06:00:00   64
    # 8 2012-01-01 07:00:00   33
    

    最后,这是MyDatesTable 对象的线图:

    plot(MyDatesTable, type="l", xlab="Time", ylab="Freq")
    


    cut 可以处理一定范围的时间间隔。例如,如果您想每 30 分钟制作一次表格,您可以轻松地调整 breaks 参数来处理它:

    data.frame(table(cut(MyDates, breaks = "30 mins")))
    #                   Var1 Freq
    # 1  2012-01-01 00:00:00   22
    # 2  2012-01-01 00:30:00   37
    # 3  2012-01-01 01:00:00   38
    # 4  2012-01-01 01:30:00   35
    # 5  2012-01-01 02:00:00   32
    # 6  2012-01-01 02:30:00   42
    # 7  2012-01-01 03:00:00   39
    # 8  2012-01-01 03:30:00   44
    # 9  2012-01-01 04:00:00   25
    # 10 2012-01-01 04:30:00   27
    # 11 2012-01-01 05:00:00   33
    # 12 2012-01-01 05:30:00   29
    # 13 2012-01-01 06:00:00   29
    # 14 2012-01-01 06:30:00   35
    # 15 2012-01-01 07:00:00   33
    

    更新

    由于您尝试使用 ggplot2 进行绘图,因此这是一种方法(不确定它是否是最好的,因为我通常在需要时使用 base R 的图形)。

    创建表的data.frame(如上所示)并添加一个虚拟“组”变量并绘制如下:

    MyDatesDF <- data.frame(MyDatesTable, grp = 1)
    ggplot(MyDatesDF, aes(Var1, Freq)) + geom_line(aes(group = grp))
    

    【讨论】:

    • @BerlinBrown,我希望它符合您的要求。我已经用 ggplot 绘制聚合数据的解决方案更新了答案。
    • 我的数据中有错误,频率为零,我收到此错误。我该如何解决这个问题。 '没有适用于'scale_dimension'的方法应用于“NULL”类的对象'
    • @BerlinBrown,如果没有一些示例数据和您经历的实际步骤,很难说。我假设您在使用cut 之前将数据转换为as.POSIXct,对吧? 所有频率都为零吗?
    • 好的,我刚刚使用了你的方法,它有效。谢谢。还有一个问题,对于 ggplot,x 轴标题中的时间重叠。你知道怎么清理吗?
    • @BerlinBrown 见here。基本步骤:(1)确保您的日期是as.POSIXct(在将表格转换为data.frame后,您必须再次转换它们;(2)加载library(scales);(3)添加一些东西喜欢last_plot() + scale_x_datetime(breaks = date_breaks("2 hour"))last_plot() + scale_x_datetime(breaks = pretty_breaks())
    猜你喜欢
    • 1970-01-01
    • 2020-05-23
    • 2018-04-27
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多