You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Fable的autoplot和autolayer中为单序列添加类似forecast的图例?

在fable中实现单模型预测图的图例显示

在forecast包中,我们可以通过autolayer的series参数快速给预测图添加图例:

library(feasts)
library(forecast)
ETS <- forecast(ets(AirPassengers), h=5)

forecast::autoplot(AirPassengers) +
  forecast::autolayer(ETS, series="ETS", PI=FALSE)

但在使用fable包时,当选择多个预测模型时,autolayer会自动生成图例:

library(fpp3)
autoplot(test) +
  autolayer(fcast |> filter(.model %in% c("stlf", "ets")), level = NULL) 

而仅选择单个模型时,图例不会自动显示:

autoplot(test) +
  autolayer(fcast |> filter(.model %in% c("stlf")), level = NULL) 

想要实现类似show_legend = TRUE的简单效果,最直接的方式是给autolayer指定series参数,手动定义图例名称,这样即使单模型也会强制显示图例:

autoplot(test) +
  autolayer(fcast |> filter(.model == "stlf"), level = NULL, series = "STLF")

这种用法和forecast包的逻辑一致,是最简洁的解决方案。

内容的提问来源于stack exchange,提问作者dolphingang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 01:10:57