我在 R Studio 中繪制時間序列資料時遇到了很多麻煩。我的資料布局如下:
tsf
Time Series:
Start = 1995
End = 2021
Frequency = 1
Jan Feb Mar Apr May Jun July Aug Sep Oct Nov Dec
1995 10817 8916 9697 10314 9775 7125 9007 6000 4155 3692 2236 996
1996 12773 12562 13479 14280 13839 9168 10959 6582 5162 4815 3768 1946
1997 14691 12982 13545 14131 14162 10415 11420 7870 6340 6869 6777 6637
1998 17192 15480 14703 16903 15921 13381 13779 9127 6676 6511 5419 3447
1999 13578 19470 23411 18190 18979 17296 16588 12561 10405 8537 7304 4003
2000 20100 29419 30125 27147 27832 23874 19728 15847 11477 9301 6933 3486
2001 16528 22258 22146 19027 19436 15688 14558 10609 6799 6563 4816 2480
2002 14724 19424 21391 17215 18775 13017 14385 10044 7649 6598 4497 2766
2003 17051 20182 18564 18484 15365 12180 13313 8859 6830 6371 3781 2012
2004 16875 20084 21150 19057 16153 13619 14144 9599 7390 5830 3763 2033
2005 20002 24153 23160 20864 18331 14950 14149 11086 7475 6290 3779 2134
2006 24605 26384 24858 20634 18951 15048 14905 10749 7259 5479 3074 1509
2007 29281 26495 25974 21427 20232 15465 15738 10006 6674 5301 2857 1304
2008 32961 24290 20190 17587 12172 7369 16175 6822 4364 2699 1174 667
2009 10996 8793 7345 5558 4840 4833 4355 2422 2272 1596 948 474
2010 10469 11707 12379 9599 8893 8314 7018 5310 4683 3742 2146 647
2011 13624 13470 12390 11171 9359 9240 6953 3653 2861 2216 1398 597
2012 14507 10993 10581 9388 7986 5481 6164 3736 2783 2442 1421 774
2013 10735 9671 10596 8113 7095 3293 9306 4504 3257 2832 1307 639
2014 15975 11906 11485 11757 7767 3390 14037 6201 4376 3082 1465 920
2015 20105 15384 17054 13166 9027 3924 21290 8572 5924 3943 1874 847
2016 27106 21173 20096 14847 10125 4143 22462 9781 5842 3831 1846 679
2017 26668 16905 17180 13427 9581 3585 21316 8105 4828 3255 1594 601
2018 25813 16501 16088 11557 9362 3716 20743 7681 4397 2874 1647 778
2019 22279 14178 14404 13794 9126 3858 18741 7202 4104 3214 1676 729
2020 20665 13263 10239 1338 1490 2189 15329 7360 5747 4189 1468 1032
2021 16948 11672 10672 8214 7337 4980 20232 8563 6354 3882 2167 832
當我嘗試使用基本代碼來繪制資料時,我得到以下資訊
plot(tsf)
'Error in plotts(x = x, y = y, plot.type = plot.type, xy.labels = xy.labels, :
cannot plot more than 10 series as "multiple"'
我的資料是每月的,因此 12 個月超過了 10 個圖表的明顯限制。我已經能夠通過排除兩個月來制作一些情節,但這對我來說不實用。我看過很多關于這個的答案,其中很多推薦ggplot() {ggplot2}
下面的鏈接中的資料與我的資料最相似,但我仍然無法應用它。
在 R 中繪制多元時間序列的問題
非常感謝任何幫助。
uj5u.com熱心網友回復:
我認為問題出在資料的形狀上。它表示Frequency = 1,表明它認為月度列是單獨的年度時間序列,而不是跨月的連續時間序列。要繪制整個時間長度,您可以重塑時間序列以匹配每月頻率(來自模擬的值資料集):
tsf_switched <- ts(as.vector(t(tsf)), start = 1995, frequency = 12)
plot(tsf)

由reprex 包創建于 2022-05-07 (v2.0.1)
uj5u.com熱心網友回復:
一個帶有 {ggplot2} 的解決方案和兩個便利庫:
library(dplyr)
library(tsbox) ## for convenient ts -> dataframe conversion
library(lubridate) ## time and date manipulation
## example monthly data for twelve years:
example_ts <- ts(runif(144), start = 2010, end = 2021, frequency = 12)
ts_data.frame(example_ts) %>% ## {tsbox}
mutate(year = year(time),
day_month = paste0(day(time),'/', month(time))
) %>%
ggplot()
geom_line(aes(day_month,
value,
group = year
)
)
將時間序列轉換為資料幀的方法(需要作為 ggplot 輸入):將時間序列轉換為資料幀并回傳
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/471577.html
下一篇:跨多個方面注釋ggplot2
