时间序列图显示不正确的前导零

时间:2019-02-11 19:21:37

标签: r plot time-series

我正在对月度数据运行时间序列,没有缺失值。无论我将“ start = c”设置为哪一年,我都会得到几个月/年的零值。

从2016年开始:

    date <- as.Date(as.yearmon(format(Vended$Year_Month),"%m_%Y"))
    timeseries <- ts(Vended$OTIF, start = c(2016), end=c(2018), 
    frequency=12)
    plot.ts(timeseries)

无开始日期:

    date <- as.Date(as.yearmon(format(VendedTS$YearMonth),"%m_%Y"))
    timeseries2 <- ts(VendedTS$OTIF,end=c(2018), frequency=12)
    plot.ts(timeseries2) 

2016年作为开始日期:

无开始日期:

以下是我的数据的第25-35行的输出(我希望如此)。

> small<- (VendedTS[25:35,])
> dput(small)
structure(list(YearMonth = c("OCT 2007", "NOV 2007", "DEC 2007", 
"JAN 2008", "FEB 2008", "MAR 2008", "APR 2008", "MAY 2008", "JUN 2008", 
"JUL 2008", "AUG 2008"), TR_CountofLines = c(40004, 33026, 38336, 
21142, 22547, 27088, 40489, 47710, 41008, 36740, 29112), TR_Value_LineItems = c(3454320.63, 
3617021.03, 4055182.81, 2471699.18, 2128728.17, 2250244.34, 3081359.17, 
3906115.74, 2932821.35, 2679508.65, 2195936.78), OTIF = c(0.28037196280372, 
0.14748985647672, 0.04249269616027, 0.15717529089017, 0.24876923759258, 
0.49556999409333, 0.51194151497938, 0.12167260532383, 0.30598907530238, 
0.22852476864453, 0.52442291838417)), row.names = c(NA, -11L), class = c("tbl_df", 
"tbl", "data.frame")

编辑:我确实希望看到2005-2007年的0值,但此后没有。

提前谢谢! 安妮

1 个答案:

答案 0 :(得分:0)

使用“小型”数据子集,尝试以下操作:

library(dplyr)
library(tidyr)
library(lubridate)
library(R.utils)

tidyr::separate(small, col = "YearMonth", into = c("Month", "Year")) %>%
  mutate(Month = R.utils::capitalize(tolower(Month))) %>%
  mutate(Day = "1") %>%
  select(Year, Month, Day, everything()) %>%
  unite(col = "Date", Year, Month, Day, sep = "-") %>%
  mutate(Date = lubridate::ymd(Date)) -> small2

timeseries <- ts(small2$OTIF, start = c(2007, 10), end = c(2008, 8), frequency = 12)
plot(timeseries)

enter image description here