rtime-seriesforecastdecomposition

How to create a distinct trend plot with AutoSTR?


I have a the following daily electric load for Spain:

> head(spain_daily)
# A tibble: 6 × 2
  Date       Daily_Load
  <date>          <dbl>
1 2022-01-01     20872.
2 2022-01-02     22729.
3 2022-01-03     27358.
4 2022-01-04     28262.
5 2022-01-05     28048.
6 2022-01-06     24182.
> tail(spain_daily)
# A tibble: 6 × 2
  Date       Daily_Load
  <date>          <dbl>
1 2024-12-26     25210.
2 2024-12-27     26363.
3 2024-12-28     25041.
4 2024-12-29     24182.
5 2024-12-30     27073.
6 2024-12-31     25924.

Since electric consumption is cyclical, I decomposed the series using the following commands:

library(forecast)
library(stR)

autoplot(mstl(msts(spain_daily$Daily_Load, seasonal.periods = c(7, 365.25))))

Which gave me the following plot:

series_decomposition

I try to recreate the plot above using the stR package's AutoSTR function using the following command:

plot(AutoSTR(msts(spain_daily$Daily_Load, seasonal.periods = c(7, 365.25))))

And it gives me the following plot:

second_decomposition

How do make this plot more similar to the first one, that is, display the trend series in its own plot, and remove the forecast (blue) series?

Thank you.


Solution

  • The first plot uses ggplot2, while the second uses base graphics. So you need to extract the components from the object produced by AutoSTR , and use ggplot2 to plot them. Fortunately, there are functions to make that easy, as shown in the following example.

    ``` r
    library(stR)
    library(forecast)
    library(ggplot2)
    
    # synthetic data
    spain_daily <- msts(
      sin(2*pi*seq(3*365)/365) + sin(2*pi*seq(3*365)/7) + rnorm(3*365, 0, 0.5),
      seasonal.periods = c(365, 7)
    )
    fit <- AutoSTR(spain_daily)
    components(fit) |>
      autoplot(colour = FALSE) +
      facet_grid(series ~ ., scales = "free_y")
    

    Created on 2025-04-11 with reprex v2.1.1