R 具有面_包络的多时间序列

R 具有面_包络的多时间序列,r,ggplot2,time-series,R,Ggplot2,Time Series,我正在做一个时间序列项目。我想将“y”数据与“y_帽”数据进行比较。我称之为“y”的是来自我的数据集的数据,“y_hat”是我的算法所预测的 我尝试过使用facet_wrap,不幸的是,它按类别绘制了一个时间序列,如图所示: 预期结果与上面的图相同,在同一个图上: *“y”和“y_hat_xgboost” *“y”和“y_hat_nnetar” *等等 所以我可以将它们与真实数据进行比较 感谢您的帮助基本上,我们需要两件事:1)不要为y单独设置一个类别,2)在其余每个类别中添加一个额外的y。因

我正在做一个时间序列项目。我想将“y”数据与“y_帽”数据进行比较。我称之为“y”的是来自我的数据集的数据,“y_hat”是我的算法所预测的

我尝试过使用facet_wrap,不幸的是,它按类别绘制了一个时间序列,如图所示:

预期结果与上面的图相同,在同一个图上:
*“y”和“y_hat_xgboost”
*“y”和“y_hat_nnetar”
*等等

所以我可以将它们与真实数据进行比较


感谢您的帮助

基本上,我们需要两件事:1)不要为
y
单独设置一个类别,2)在其余每个类别中添加一个额外的
y
。因此,

ggplot(gather_df %>% filter(Algorithm != "y"), aes(x = ds, y = values)) +
  geom_line(aes(color = Algorithm)) +
  scale_color_brewer(palette = "Dark2") +
  facet_wrap(~ Algorithm) + 
  geom_line(data = gather_df %>% filter(Algorithm == "y") %>% select(-Algorithm))
我们实现了这一点,其中,
gather\u df%>%filter(算法!=“y”)
执行第1部分),最后一行执行第2部分)

如果您希望
y
曲线出现在图例中,您也可以在最后一行添加,例如,
aes(color=“y”)
。这给

如果您想在每个面板上显示“y”,您不想收集它。试试这个:

nb_of_algorithm <- 6
gather_df <- df_all %>% 
    gather(key="Algorithm", "values", 2:nb_of_algorithm, -y)

ggplot(gather_df, aes(x = ds, y = values)) +
    geom_line(aes(color = Algorithm)) +
    geom_line(aes(y = y), colour = "black") +
    scale_color_brewer(palette="Dark2") +
    facet_wrap(~ Algorithm)
nb\u算法
ggplot(gather_df %>% filter(Algorithm != "y"), aes(x = ds, y = values)) +
  geom_line(aes(color = Algorithm)) +
  scale_color_brewer(palette = "Dark2") +
  facet_wrap(~ Algorithm) + 
  geom_line(data = gather_df %>% filter(Algorithm == "y") %>% select(-Algorithm))
nb_of_algorithm <- 6
gather_df <- df_all %>% 
    gather(key="Algorithm", "values", 2:nb_of_algorithm, -y)

ggplot(gather_df, aes(x = ds, y = values)) +
    geom_line(aes(color = Algorithm)) +
    geom_line(aes(y = y), colour = "black") +
    scale_color_brewer(palette="Dark2") +
    facet_wrap(~ Algorithm)