rforecastingautoplotggseasonplot

Time series data in R - seasonal plot using ggseasonalplot and autoplot


I have two months daily time series data starting from 1st january 2025.The data shows high value on weekdays and dip on sat and sunday. Also weekdays of 1st two weeks of the month have high value as compared to last two weeks

while plotting I have two issues here:

  1. The seasonal plot shows Thursday as the lowest. It's actually sundays
  2. How to get the actual date on x axis using autoplot. Using ggplot it works fine though

Below is the code:

   library(forecast)

   dates <- seq(as.Date("2025-01-01"),as.Date("2025-02-28"),by = 1)
   residual <- c(1272265118, 1893878984, 1971248967, 1206920994, 1071775081, 2113202354,
          2096828782,2156692644, 2066645508, 2104943137, 1482914237,  635563876, 
          1553731395,  985106749,1234648105, 1222932743, 1266615010,  552150792,
          212894667, 1122524796, 1148910414,
          1087774137, 1019459908, 1046319082,  586277591,  153830967,  994011343,
          1016875440,
          1015254239, 1011042302, 1104754682,608339645,  692436369, 1860344171,
          1987819504,
          2157579522, 2091920934, 1993873640, 1580583560, 1026390430, 
          2196973517, 1853411958,
          1735132015, 1490268789, 1345789623,  720690501,  326810024,
          1244604895, 1210696838,
          1493402864, 1126684981,1093146463,  613249936,  198156734, 1067010013,
          1044503832,
          1209633127, 1080260309, 1159072875)
   data <- data.frame(dates,residual)
   ts_data <- ts(data$residual,start = as.Date("2025-01-01"),frequency = 7)
   autoplot(ts_data)
   ggseasonplot(ts_data) 

Solution

  • You are misunderstanding the start argument in ts(). It does not take a date, but a number. But you are probably better off using the tsibble and feasts package which are much better for daily data. You can get the plots you want as follows:

    ts_data <- as_tsibble(data, index = dates)
    autoplot(ts_data, residual)
    gg_season(ts_data, residual, "week")
    

    If you don't like the automated axis scales or labels, you can just modify the result using ggplot2 commands in the usual way,