首页 > 解决方案 > 从 R 中的 tibble 创建时间序列对象以生成 ggseasonplot

问题描述

我正在尝试从一年的每日时间序列数据ts中创建一个时间序列对象。数据是一个小标题,看起来像这样:Rsingle

       Year  Month Day   Simple_Assault
       <chr> <chr> <chr>          <int>
     1 2020  01    01                 6
     2 2020  01    02                 6
     3 2020  01    05                 4
     4 2020  01    06                 4
     5 2020  01    07                 1
     6 2020  01    08                 5
     7 2020  01    09                 4
     8 2020  01    10                 2
     9 2020  01    11                 5
    10 2020  01    12                 2
# ... with 102 more rows

ts创建对象时出现错误。这个想法是创建一个时间序列对象以生成ggseasonplot类似于下图所示的对象:

在此处输入图像描述

代码

    library(tidyverse)
    
    # Create a ts object from a tibble
    data_ts = ts(data, start = 2020, end = 2020, frequency = 8) 
    Error in attr(data, "tsp") <- c(start, end, frequency) : 
      invalid time series parameters specified

    # Getting an error possibly due to incorrect argument use

标签: rggplot2

解决方案


我首先犯了两个错误,我给了一个错误的值frequency,因为我有一个每日时间序列,我应该使用12频率。其次,start论点也必须修改。

代码

# Create time series 
data_ts = ts(crime_2701,start = c(2020,1), frequency =12)

# Check
plot(data_ts)

# Create a season plot
ggseasonplot(data_ts[,4], polar=TRUE) 

在此处输入图像描述


推荐阅读