请大佬帮忙看下,谢谢~
最近在看时间序列分析,使用forecast
包里的autoplot
进行绘图时,加上scale_x_date(date_break = "1 days", date_labels = "%b %d")
就报错。
测试的数据MA May
>raw_data<-read_xlsx("MA May.xlsx")
使用默认参数画图是正常的:
>MA_May<-xts(raw_data$Delta_Close, raw_data$Date)
加上自定义参数后就报错:
>autoplot(MA_May) + geom_abline(color = "blue") +
scale_x_date(date_break = "1 days", date_labels = "%b %d")
Error in `transformation$transform()`:
! `transform_date()` works with objects of class <Date> only
Run `rlang::last_trace()` to see where the error occurred.
使用class
查看Date列,确实读取为时间格式了啊:
> class(raw_data$Date)
[1] "POSIXct" "POSIXt"
运行了下rlang::last_trace()
,没看明白:
Backtrace:
▆
1. ├─base (local) `<fn>`(x)
2. └─ggplot2:::print.ggplot(x)
3. ├─ggplot2::ggplot_build(x)
4. └─ggplot2:::ggplot_build.ggplot(x)
5. └─base::lapply(data, scales$transform_df)
6. └─ggplot2 (local) FUN(X[[i]], ...)
7. └─ggplot2 (local) transform_df(..., self = self)
8. ├─base::unlist(...)
9. └─base::lapply(scales, function(scale) scale$transform_df(df = df))
10. └─ggplot2 (local) FUN(X[[i]], ...)
11. └─scale$transform_df(df = df)
12. └─ggplot2 (local) transform_df(..., self = self)
13. └─base::lapply(df[aesthetics], self$transform)
14. └─ggplot2 (local) FUN(X[[i]], ...)
15. └─ggplot2 (local) transform(..., self = self)
16. └─transformation$transform(x)
17. └─cli::cli_abort("{.fun transform_date} works with objects of class {.cls Date} only")
18. └─rlang::abort(...)