簡體   English   中英

按組和多個變量在 R 中繪制時間序列

[英]Plotting Time Series in R by group and with multiple variables

如何在 R 中獲得最佳和簡單的時間序列圖:按(7)省分組,並顯示 4 個變量(BI、PD、AP、COLL.)。 我想單獨繪制組。

我試過plot_time_series()的函數timetk包,但我一直搞亂沒有給我我想要的。

在此處輸入圖片說明

  • 這是我的數據:
dput(data)
structure(list(Province = c("Alberta", "Alberta", "Alberta", 
"Alberta", "Alberta", "Alberta", "Alberta", "Alberta", "Alberta", 
"Alberta", "Alberta", "Alberta", "Alberta", "Alberta", "Alberta", 
"Alberta", "Alberta", "Alberta", "NewBrunswick", "NewBrunswick", 
"NewBrunswick", "NewBrunswick", "NewBrunswick", "NewBrunswick", 
"NewBrunswick", "NewBrunswick", "NewBrunswick", "NewBrunswick", 
"NewBrunswick", "NewBrunswick", "NewBrunswick", "NewBrunswick", 
"NewBrunswick", "NewBrunswick", "NewBrunswick", "NewBrunswick", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NewfoundlandandLabrador", "NewfoundlandandLabrador", "NewfoundlandandLabrador", 
"NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", 
"NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", 
"NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", "NovaScotia", 
"NovaScotia", "NovaScotia", "NovaScotia", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "PrinceEdwardIsland", 
"PrinceEdwardIsland", "PrinceEdwardIsland", "Ontario", "Ontario", 
"Ontario", "Ontario", "Ontario", "Ontario", "Ontario", "Ontario", 
"Ontario", "Ontario", "Ontario", "Ontario", "Ontario", "Ontario", 
"Ontario", "Ontario", "Ontario", "Ontario", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr", 
"Yukon,Nunavut,NWTerr", "Yukon,Nunavut,NWTerr"), AccPeriod = structure(c(NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date"), 
    BI = c(1.092, 1.117, 1.177, 1.235, 1.331, 1.465, 1.645, 1.851, 
    1.048, 1.073, 1.107, 1.16, 1.245, 1.372, 1.572, 1.812, 2.097, 
    2.901, 1.09, 1.123, 1.144, 1.217, 1.334, 1.463, 1.586, 1.729, 
    1.089, 1.115, 1.14, 1.21, 1.289, 1.418, 1.604, 1.787, 1.946, 
    2.508, 1.012, 1.016, 1.031, 1.049, 1.091, 1.117, 1.162, 1.226, 
    1, 1, 0.998, 1.008, 1.025, 1.059, 1.113, 1.176, 1.285, 2.055, 
    1.025, 1.042, 1.072, 1.12, 1.185, 1.276, 1.391, 1.495, 1.04, 
    1.062, 1.094, 1.128, 1.202, 1.292, 1.412, 1.559, 1.771, 2.361, 
    1.056, 1.063, 1.131, 1.149, 1.211, 1.308, 1.407, 1.452, 1.025, 
    1.037, 1.07, 1.072, 1.146, 1.254, 1.366, 1.468, 1.574, 2.045, 
    1.009, 1.012, 1.024, 1.054, 1.105, 1.192, 1.371, 1.552, 0.995, 
    1.003, 1.017, 1.053, 1.11, 1.214, 1.398, 1.627, 1.893, 2.76, 
    1.092, 1.117, 1.177, 1.235, 1.331, 1.465, 1.645, 1.851, 1.048, 
    1.073, 1.107, 1.16, 1.245, 1.372, 1.572, 1.812, 2.097, 2.901
    ), PD = c(1, 1, 1, 1.002, 1.001, 1.001, 1.005, 1.02, 1, 1, 
    1, 1, 1.001, 1.001, 1.005, 1.027, 1.059, 1.311, 0.999, 0.998, 
    0.998, 0.999, 1.003, 1, 1.004, 1.033, 1.002, 1.002, 1.002, 
    0.998, 0.999, 1.009, 1.035, 1.092, 1.187, 1.195, 1, 1, 1, 
    0.997, 0.994, 0.995, 0.988, 0.996, 1.001, 0.997, 0.998, 0.998, 
    0.999, 0.999, 1.005, 1.019, 1.059, 1.398, 1, 1, 1, 1, 1.002, 
    1.003, 1.005, 1.01, 1, 1, 1, 1.007, 1.008, 1.007, 1.013, 
    1.091, 1.147, 1.048, 1, 1, 1, 1, 1, 1, 1.005, 1.018, 1, 1, 
    1, 1, 1, 1, 1.054, 1.116, 1.136, 1.144, 1, 1, 1, 1, 1.002, 
    1.012, 1.077, 1.179, 1, 1, 1, 1.004, 1.01, 1.03, 1.099, 1.225, 
    1.479, 2.077, 1, 1, 1, 1.002, 1.001, 1.001, 1.005, 1.02, 
    1, 1, 1, 1, 1.001, 1.001, 1.005, 1.027, 1.059, 1.311), Coll. = c(1, 
    1, 1, 1, 0.999, 0.996, 0.975, 0.932, 1, 1, 1, 1, 1, 0.998, 
    0.989, 0.964, 0.885, 0.652, 1, 1, 1, 1, 1, 1, 1, 0.998, 1, 
    1, 1, 1, 1, 1, 1, 1, 0.996, 0.974, 1, 1, 1, 1, 1, 0.997, 
    0.989, 0.974, 1, 1, 1, 1, 1, 0.999, 0.991, 0.972, 0.903, 
    0.792, 1, 1, 1, 1, 1, 0.999, 0.997, 0.993, 1, 1, 1, 1, 1, 
    1, 1, 0.999, 0.995, 0.984, 1, 1, 1, 1, 0.999, 0.998, 0.991, 
    0.977, 1, 1, 1, 1, 1, 0.999, 0.999, 0.998, 0.99, 0.968, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1.001, 1.002, 1.004, 
    1.042, 1, 1, 1, 1, 0.999, 0.996, 0.975, 0.932, 1, 1, 1, 1, 
    1, 0.998, 0.989, 0.964, 0.885, 0.652), AllPerils = c(1, 1, 
    1, 1, 1, 1.001, 0.995, 0.974, 1, 1, 1, 1, 0.995, 0.996, 0.986, 
    0.965, 0.905, 0.842, 1, 1, 1, 1, 1, 1, 0.997, 0.996, 1, 1, 
    1, 1, 1, 1, 1, 1, 0.999, 0.998, 1, 1, 1, 1, 1.002, 0.996, 
    0.987, 0.979, 1, 1, 1, 1, 0.999, 0.997, 0.99, 0.963, 0.917, 
    0.786, 1, 1, 1, 1, 0.998, 0.999, 0.997, 0.988, 1, 1, 1, 1, 
    1, 0.999, 1, 0.999, 0.994, 0.996, 1, 1, 1, 1, 1, 1, 0.998, 
    0.991, 1, 1, 1, 1, 1, 1, 1.001, 0.997, 1, 1.023, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 0.999, 0.999, 0.999, 0.999, 1, 
    1.045, 1, 1, 1, 1, 1, 1.001, 0.995, 0.974, 1, 1, 1, 1, 0.995, 
    0.996, 0.986, 0.965, 0.905, 0.842)), row.names = c(NA, -126L
), class = c("tbl_df", "tbl", "data.frame"))

在您上面的數據中,缺少AccPeriod數據。 因此,根據您的屏幕截圖,我快速重新創建了它,以使用兩年一次的數據從 2012 年到 2021 年。

library(tidyverse)
data %>% 
  group_by(Province) %>% 
  mutate(AccPeriod = as.Date(c("2012-01-01", "2012-07-01", "2013-01-01", "2013-07-01", "2014-01-01", 
                       "2014-07-01", "2015-01-01", "2015-07-01", "2016-01-01", "2016-07-01", 
                       "2017-01-01", "2017-07-01", "2018-01-01", "2018-07-01", "2019-01-01", 
                       "2019-07-01", "2020-01-01", "2020-07-01"))) %>% 
  ungroup -> data

現在我們有了我們需要的數據,我們首先需要將它重新格式化為一個長數據幀:

# reformat to long format
data %>% 
  pivot_longer(-c(AccPeriod, Province)) -> data

現在我們可以繪制它(注意paste0(Province,name)它只是確保我們按名稱對每一行進行分組,即變量和省份):

ggplot(data) + 
  aes(x = AccPeriod, y = value, group = paste0(Province,name)) + 
  geom_line()

這有效,但生成的圖形不是很好。

在此處輸入圖片說明

讓我們給它加點料:我們首先確保每個變量都有自己的小圖,每個省都有自己的顏色:

ggplot(data) + 
  aes(x = AccPeriod, y = value, group = paste0(Province,name), color = Province) + 
  geom_line() + 
  facet_wrap(~name, scales = "free") # scales free as the units are different (makes it look nicer)

在此處輸入圖片說明

或者反過來:

ggplot(data) + 
  aes(x = AccPeriod, y = value, group = paste0(Province,name), color = name) + 
  geom_line() + 
  facet_wrap(~Province) # scales fixed as the units are the same (makes it more comparable)

在此處輸入圖片說明

暫無
暫無

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

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