将RMSE和MAE粘贴到ggplot2中的预测旁边

将RMSE和MAE粘贴到ggplot2中的预测旁边,r,ggplot2,R,Ggplot2,我有一些地方的产品,我需要预测。大约有300个位置的产品组合。一个绘图始终是位置和所有产品(如图中所示)。现在,我想在绘图中图形旁边打印每个产品的RMSE和MAE。它们被计算并存储在各自的列中的data\u accurity,但我不知道如何将它们打印到绘图中。有可行的方法吗 Ps:我知道越界错误。这只是发生在这个示例数据中,但结果正常 library(tidyverse) library(tsibble) library(fable) library(lubrid

我有一些地方的产品,我需要预测。大约有300个位置的产品组合。一个绘图始终是位置和所有产品(如图中所示)。现在,我想在绘图中图形旁边打印每个产品的RMSE和MAE。它们被计算并存储在各自的列中的
data\u accurity
,但我不知道如何将它们打印到绘图中。有可行的方法吗

Ps:我知道越界错误。这只是发生在这个示例数据中,但结果正常

    library(tidyverse)
    library(tsibble)
    library(fable)
    library(lubridate)
    
    data_ts <- data.frame(sales_year = c("2015-01-01", "2015-01-01", "2015-01-01", "2016-01-01",
 "2016-01-01", "2016-01-01", "2017-01-01", "2017-01-01", "2017-01-01", "2018-01-01", "2018-01-01",
 "2018-01-01", "2019-01-01", "2019-01-01", "2019-01-01"), product = c("a", "b", "c", "a", "b", "c",
 "a", "b", "c", "a", "b", "c", "a", "b", "c"), sales = c(6, 11, 13, 6, 2, 6, 11, 12, 10,  4, 12, 2,
 17, 6, 8))
    data_ts <- data_ts%>%
      mutate(sales_year = year(sales_year)) 
    
    data_ts <- tsibble(data_ts, index = sales_year, key = product)
    
    data_train <- data_ts %>%
      filter(sales_year < "2018-01-01")
    
    data_ses <- data_train %>%
      model(ETS(sales ~ error("A") + trend("N") + season("N")))
    data_ses_fc <- data_ses %>%
      forecast(h = 1)
    
    data_accuracy <- data_ses_fc %>%
      accuracy(data_ts)
    
    data_ses_fc %>%
      autoplot(data_ts) +
      geom_line(aes(y = .fitted), col="red",
                data = augment(data_ses))
库(tidyverse)
图书馆(TSIBLE)
图书馆(寓言)
图书馆(lubridate)
像这样的数据

data_accuracy <- data_ses_fc %>%
  accuracy(data_ts) %>% 
  mutate(res = paste0("RMSE: ",round(RMSE,2),"\n",
                      "MAE: ",round(MAE,2)))

data_ses_fc %>%
  autoplot(data_ts) +
  geom_line(aes(y = .fitted), col="red",
            data = augment(data_ses)) +
  geom_text(data=data_accuracy,aes(x = 2016,y=12,group=product,label=res))
数据精度%
准确度(数据)%>%
变异(res=paste0(“RMSE:”,round(RMSE,2),“\n”,
“梅:”,圆(梅,2)))
数据使用率%fc%>%
自动绘图(数据)+
几何尺寸线(aes(y=.fitted),col=“红色”,
数据=增加(数据)+
几何图形文本(数据=数据精度,aes(x=2016,y=12,组=产品,标签=分辨率))

这很好用。我希望把它写在图表之外,但这也行得通。谢谢你的努力!我通过增加x来解决这个问题,这样就可以很好地拟合数据。