簡體   English   中英

如何使用寓言 package 將 geom_line 連接到預測風扇

[英]How to connect geom_line to forecast fan using the fable package

我有一個 plot 的以下代碼,使用fable package 進行預測。 我希望找到一個解決方案,我可以將預測風扇連接到實際的geom_line ,這樣兩者之間就沒有差距。 另外,有沒有辦法改變風扇的顏色? 例如,我嘗試在autoplot括號中添加col = 'maroon' ,但這似乎不起作用。

這是我的數據集cpi的可復制品

structure(list(Date = structure(c(1356998400, 1359676800, 1362096000, 
1364774400, 1367366400, 1370044800, 1372636800, 1375315200, 1377993600, 
1380585600, 1383264000, 1385856000, 1388534400, 1391212800, 1393632000, 
1396310400, 1398902400, 1401580800, 1404172800, 1406851200, 1409529600, 
1412121600, 1414800000, 1417392000, 1420070400, 1422748800, 1425168000, 
1427846400, 1430438400, 1433116800, 1435708800, 1438387200, 1441065600, 
1443657600, 1446336000, 1448928000, 1451606400, 1454284800, 1456790400, 
1459468800, 1462060800, 1464739200, 1467331200, 1470009600, 1472688000, 
1475280000, 1477958400, 1480550400, 1483228800, 1485907200, 1488326400, 
1491004800, 1493596800, 1496275200, 1498867200, 1501545600, 1504224000, 
1506816000, 1509494400, 1512086400, 1514764800, 1517443200, 1519862400, 
1522540800, 1525132800, 1527811200, 1530403200, 1533081600, 1535760000, 
1538352000, 1541030400, 1543622400, 1546300800, 1548979200, 1551398400, 
1554076800, 1556668800, 1559347200, 1561939200, 1564617600, 1567296000, 
1569888000, 1572566400, 1575158400, 1577836800, 1580515200, 1583020800, 
1585699200, 1588291200, 1590969600, 1593561600), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), CPI = c(100.943613610327, 101.355726290109, 
101.920519704091, 102.251765014058, 102.399483334481, 102.654230611209, 
103.366370423635, 103.771996583604, 104.069828647932, 104.475897454947, 
104.745585890252, 104.9, 105.877675706645, 106.600613244374, 
107.25658797107, 108.285287342243, 108.607710827378, 108.935592526775, 
109.11670321665, 109.390661099815, 109.563232156331, 109.694215435852, 
109.939646273932, 109.754097918499, 110.601049654351, 110.415206179718, 
110.905507883552, 111.45837834832, 111.873469766967, 112.253828314821, 
112.699336213665, 113.056054221625, 113.204653466884, 113.387164759728, 
113.581282843726, 113.810860009533, 116.506784014018, 117.199721025597, 
118.107968739773, 118.823678758349, 119.420709143437, 119.808600479962, 
120.575551335206, 120.774779709305, 121.014544917053, 121.61732414169, 
121.917354377998, 122.116542025261, 126.058371342546, 126.285551233707, 
126.43426615261, 126.763103151148, 126.92061331762, 127.095652703716, 
127.146439944094, 127.257270861715, 127.754395868046, 127.897364611267, 
128.227889139291, 128.426778898969, 130.540032633942, 130.730222134177, 
130.87769195147, 131.302356289165, 131.797387843531, 132.126557217198, 
132.823218725753, 132.868685232286, 133.870800057958, 134.439906096246, 
135.351580975176, 135.040382301698, 136.620612224767, 136.503608878263, 
136.763944144826, 137.24925661824, 137.169191683167, 137.331600194512, 
137.656945057261, 137.792027588476, 137.792027588476, 138.493686354623, 
138.681976535356, 138.535078801086, 139.421769773802, 139.848223614133, 
139.983926150073, 139.504431667605, 139.994961370897, 140.280481556844, 
140.529583177439)), row.names = c(NA, -91L), class = c("tbl_df", 
"tbl", "data.frame"))



這是我的 model + plot 的實際代碼

library(tidyverse)
library(tsibble)
library(fable)


cpi <- cpi %>% 
  mutate(Date = yearmonth(Date)) %>%
  as_tsibble(index=Date)


fit <- cpi %>%
  model(arima = ARIMA(log(CPI)))

fit %>%
  forecast(h="1 year") %>%
  autoplot(cpi %>% slice(tail(row_number(), 12)), level=seq(10,90,by=10)) +
  theme(legend.position="none")

在此處輸入圖像描述

這可以通過select(slice(., 1), Date, CPI =.mean))從預測返回的 df 中獲取長度為 1 的切片並通過dplyr::bind_rows()將其綁定到您的數據來實現。 用於綁定兩個數據集 select 僅列Date和。 mean來自 model df 並將.mean重命名為CPI 關於風扇的填充顏色。 這可以通過操縱自動繪圖返回的 ggplot object 來實現。 使用反復試驗的方法,我設法通過p$layers[[1]]$aes_params$fill <- "red"更改填充顏色。 嘗試這個:

library(tidyverse)
library(tsibble)
library(fable)
#> Loading required package: fabletools


cpi <- cpi %>% 
  mutate(Date = yearmonth(Date)) %>%
  as_tsibble(index=Date)

fit <- cpi %>%
  model(arima = ARIMA(log(CPI)))

p <- fit %>%
  forecast(h="1 year") %>%
  autoplot(bind_rows(cpi %>% slice(tail(row_number(), 12)), select(slice(., 1), Date, CPI = .mean)), level=seq(10,90,by=10), show_gap = TRUE) +
  theme(legend.position="none")

p$layers[[1]]$aes_params$fill <- "red"

p

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM