[英]Multiple time-series with face_wrap
I am working on a time-series project. 我正在做一个时间序列项目。 I would like to compare the "y" data to the "y_hat".
我想将“ y”数据与“ y_hat”进行比较。 What I call "y" is the data from my dataset and "y_hat" is what my algorithms have predicted.
我所说的“ y”是我的数据集中的数据,而“ y_hat”是我的算法所预测的。
I have tried using facet_wrap, unfortunately it plots one time-series by category as you can see on the image : 我尝试使用facet_wrap,不幸的是,如您在图像上看到的,它按类别绘制了一个时间序列:
nb_of_algorithm <- 6
gather_df <- df_all %>% gather(key="Algorithm", "values", 2:nb_of_algorithm)
ggplot(gather_df, aes(x = ds, y = values)) +
geom_line(aes(color = Algorithm)) +
scale_color_brewer(palette="Dark2") +
facet_wrap(~ Algorithm)
And I add a sample of what my dataframe looks like 我添加了一个数据框外观示例
ds Algorithm values
1 2018-10-19 y 8115.000
2 2018-10-20 y 8730.000
3 2018-10-21 y 7155.000
4 2018-10-22 y 570.000
164 2018-10-19 y_hat_xgboost 3458.394
165 2018-10-20 y_hat_xgboost 6424.176
166 2018-10-21 y_hat_xgboost 3416.893
167 2018-10-22 y_hat_xgboost 12041.853
168 2018-10-23 y_hat_xgboost 9801.245
169 2018-10-24 y_hat_xgboost 11081.888
327 2018-10-19 y_hat_nnetar 7188.586
328 2018-10-20 y_hat_nnetar 6606.201
329 2018-10-21 y_hat_nnetar 10488.071
330 2018-10-22 y_hat_nnetar 17417.546
331 2018-10-23 y_hat_nnetar 14230.000
The expected results would be the same graph as above, with on the same plot: 预期结果将是与上述相同的图,并且在相同的图上:
* "y" and "y_hat_xgboost" *“ y”和“ y_hat_xgboost”
* "y" and "y_hat_nnetar" *“ y”和“ y_hat_nnetar”
* and so on ... * 等等 ...
So I can compare them to the real data 所以我可以将它们与真实数据进行比较
Thanks for your help 谢谢你的帮助
Essentially we want two things: 1) not to have a separate category for y
, 2) to add an extra layer of y
in each of the remaining categories. 本质上,我们想要两件事:1)不要为
y
设置单独的类别,2)在其余的每个类别中添加y
的额外层。 Hence, with 因此,随着
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))
we achieve that, where gather_df %>% filter(Algorithm != "y")
does part 1) and the last line does 2). 我们实现了这一点,其中
gather_df %>% filter(Algorithm != "y")
做第1部分),最后一行做2)。
If you wish the curve of y
to appear in the legend, you may also add, say, aes(color = "y")
to the last line. 如果希望
y
的曲线出现在图例中,则也可以在最后一行添加aes(color = "y")
。 This gives 这给
If you want "y" on every panel you don't want to gather it. 如果在每个面板上都希望“ y”,则不想收集它。 Try this:
尝试这个:
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)
I don't have the data so I could't try it out, but I hope it at least gets you in the right direction. 我没有数据,所以无法尝试,但我希望至少可以帮助您朝正确的方向发展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.