簡體   English   中英

使用face_wrap的多個時間序列

[英]Multiple time-series with face_wrap

我正在做一個時間序列項目。 我想將“ y”數據與“ y_hat”進行比較。 我所說的“ y”是我的數據集中的數據,而“ y_hat”是我的算法所預測的。

我嘗試使用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)

我添加了一個數據框外觀示例

            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

預期結果將是與上述相同的圖,並且在相同的圖上:
*“ 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_df %>% filter(Algorithm != "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)

我沒有數據,所以無法嘗試,但我希望至少可以幫助您朝正確的方向發展。

暫無
暫無

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

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