packages distributional and fabletools updates to 0.4.0: plotting functions (in)consistencies

Hi Rob and Mitch

Following the recent updates to 0.4.0 regarding both packages: distributional and fabletools, there's some differences in the consistencies between the plotting functions.

Below are 2 examples generating forecasts based on the data in fpp3:
(1) static
(2) dynamic

For static forecasts I previously used geom_hilo_ribbon, which has been deprecated and instead there's ggdist::stat_lineribbon or ggdist::geom_lineribbon.

I would like the stretch_tsibble static one-step ahead forecast graphs to be same as the default autoplot dynamic multistep ahead graphs.

The differences being for the static one-step ahead versus the default autoplot dynamic multistep ahead:
(a) Not exactly the same colour. What are the default autoplot dynamic multistep ahead colours?
(b) Being see through in colour, i.e. can see the grid in the background. How do I make the colours not see-through or opaque in plot_S?
(c) The level key on the RHS is 0.95 and 0.8 and both the same colour, whereas the default autoplot is 80% and 95% respectively and different colours.

i.e. How do I change plot_S to match plot_D?

#=============================== 
library(fpp3)
library(ggdist)


## ---- global economy--------------------------------------------------------
aus_economy <- global_economy |>
  filter(Code == "AUS") |>
  mutate(Population = Population / 1e6)


fc_st <- aus_economy |>
  slice(-n()) |>
  stretch_tsibble(.init = 40) |>
  model(ETS(log(Population))) |>
  forecast(h = 1, point_forecast = lst(.mean = mean, .median = median), bootstrap = TRUE)
fc_st


Pop <- global_economy |>
  filter(Code == "AUS") |>
  mutate(Population = Population / 1e6) |>
  select(Population)


plot_S <- fc_st |>
  ggplot(aes(x = Year, ydist = Population)) +
  stat_lineribbon(
    .width = c(0.8, 0.95),
    color = "blue",
    fill = "blue",
    alpha = 0.2,
    show.legend = TRUE,
    lwd = 0.1
  ) +
  scale_fill_brewer() +
  geom_line(aes(y = .mean), color = "blue", linetype = "solid") +
  geom_line(aes(y = .median, color = "median"), color = "blue", linetype = "dotted") +
  geom_line(data = Pop, aes(x = Year, y = Population)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text = element_text(size = 14)) +
  theme(plot.caption = element_text(hjust = 0)) + # set the left align here
  labs(caption = "bootstrapped residuals") +
  ggtitle("Population\nYRECURSIVE Static one-step ahead Forecasts and Prediction Intervals ETS")
plot_S


#=============================== 
#Series for IS period
y <- Pop |> filter(Year >= '1960' & Year <= '1999')


#fit ETS
fit <- y |>
  model('ets' = ETS(log(Population)))
fit


#dynamic forecasts
fc_D <- fit |>
  forecast(h = 18, point_forecast = lst(.mean = mean, .median = median), bootstrap = TRUE)
fc_D


#dynamic forecasts & PI's
fcPI_D <- fc_D |> hilo()
fcPI_D


#fc_D fanchart  
plot_D <- fc_D |>
  autoplot(y) +
  geom_line(aes(y = .median), col = "blue", data = fc_D, linetype = "dashed") +
  geom_line(data = Pop, aes(x = Year, y = Population)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text = element_text(size = 14)) +
  theme(plot.caption = element_text(hjust = 0)) + # set the left align here
  labs(caption = "bootstrapped residuals") +
  ggtitle("Population\nYRECURSIVE Dynamic h-steps ahead Forecasts and Prediction Intervals ETS")
plot_D

Also I have also noticed from the following:

#=============================== 
fit <- y |>
  model(
    naive    = ARIMA(Population ~ 0 + pdq(0, 1, 0)), # NAIVE(value),
    rw_drift = ARIMA(Population ~ 1 + pdq(0, 1, 0)) # RW(value ~ drift())
  )
fit


fc_simple_D <- fit |> 
  forecast(h = 20, point_forecast = lst(.mean = mean), bootstrap = TRUE)
fc_simple_D


fc_simple_D |>
  autoplot(tail(y, 20), point_forecast = lst(.mean = mean))

(i )The .model key on the RHS is blocked in colour, whereas they used to have a diagonal time line for each model, as here Key glyphs for legends — draw_key • ggplot2 (tidyverse.org). How do I get the timeseries key glyph in the default autoplot?
(ii) The new blocked colour(s) in the key are different to the actual fan(s) colours.

thanks,
Amarjit

Hi Rob and Mitch,

I would very much appreciate guidance on the above.

Specifically:

[A] How do I change plot_S to look like the default plot_D, regarding
(a) exact colours
(b) opaqueness
(c) and the key being 80% and 95%?

[B] Further, I have discovered the following

#=============================== 
fc_st <- prices |>
  filter(!is.na(eggs)) |>
  stretch_tsibble(.init = 80) |>
  model(RW(log(eggs) ~ drift())) |>
  forecast(h = 1, point_forecast = lst(.mean = mean, .median = median), bootstrap = TRUE)
fc_st
  

eggs <- prices |>
  filter(!is.na(eggs)) |>
  select(eggs)


plot_S <- fc_st |>
  ggplot(aes(x = year, ydist = eggs)) +
  stat_lineribbon(
    .width = c(0.8, 0.95),
    color = "blue",
    fill = "blue",
    alpha = 0.2,
    show.legend = TRUE,
    lwd = 0.1
  ) +
  scale_fill_brewer() +
  #geom_line(aes(y = .mean), color = "red", linetype = "solid") +
  #geom_line(aes(y = .median, color = "median"), color = "green", linetype = "dotted") +
  geom_line(data = tail(eggs,20), aes(x = year, y = eggs)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text = element_text(size = 14)) +
  theme(plot.caption = element_text(hjust = 0)) + # set the left align here
  labs(caption = "bootstrapped residuals") +
  ggtitle("eggs\nYRECURSIVE Static one-step ahead Forecasts and Prediction Intervals RW")
plot_S

plot_S plots the .median (not the .mean) as a feint blue line.

If I now include

 geom_line(aes(y = .mean), color = "red", linetype = "solid") +

the .mean line is plotted as expected (the mean forecasts are higher than the median forecasts),

and including

 geom_line(aes(y = .median, color = "median"), color = "green", linetype = "dotted") +

the initial feint blue line (the .median) is overwriiten.

Why is the .median automatically plotted with ggplot? I appreciate the median is the "natural" point forecast from a log transformation, but point_forecast = lst(.mean = mean, .median = median), respectively i.e. mean first.

Also, without a log transformation and bootstrap = TRUE

plot_S <- fc_st |>
  ggplot(aes(x = year, ydist = eggs)) +
  stat_lineribbon(
    .width = c(0.8, 0.95),
    color = "blue",
    fill = "blue",
    alpha = 0.2,
    show.legend = TRUE,
    lwd = 0.1
  ) +
  scale_fill_brewer() +
  geom_line(aes(y = .mean), color = "red", linetype = "solid") +
  geom_line(data = tail(eggs,20), aes(x = year, y = eggs)) +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text = element_text(size = 14)) +
  theme(plot.caption = element_text(hjust = 0)) + # set the left align here
  labs(caption = "bootstrapped residuals") +
  ggtitle("eggs\nYRECURSIVE Static one-step ahead Forecasts and Prediction Intervals RW")
plot_S

results in 2 forecast .mean lines. Why? My aim being to make the ggplot feint .mean line better visible and thicker, how if I don't include geom_line?

But, with bootstrap = FALSE, there is only the 1 .mean forecast line, which is correct.

All-in-all, I am requesting a default plot (e.g. plot_S) for static, stretch_tsibble e.g. one-step ahead forecasts!

thanks,
Amarjit

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.