繁体   English   中英

r - 在 R 中绘制每月时间序列数据 - plot 不能超过 10 个系列

[英]r - Plotting monthly time series data in R - cannot plot more than 10 series

我在 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 时,我得到以下数据

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 个图表的明显限制。我已经能够通过排除两个月来制作一些 plot 但这对我来说不切实际。 我看过很多关于这个的答案,其中很多推荐ggplot() {ggplot2}

下面的链接中的数据与我的数据最相似,但我仍然无法应用它。

在 R 中绘制多元时间序列的问题

非常感谢任何帮助。

我认为问题出在数据的形状上。 它表示Frequency = 1 ,表明它认为每月列是单独的年度时间序列,而不是跨月的连续时间序列。 到 plot 整个时间长度,您可以重塑时间序列以匹配每月频率(来自模拟的值数据集):

tsf_switched <- ts(as.vector(t(tsf)), start = 1995, frequency = 12)

plot(tsf)

reprex package (v2.0.1) 创建于 2022-05-07

{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 输入): 将时间序列转换为数据帧并返回

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM