gpt4 book ai didi

r - 在 R 中的时间序列数据图的 X 轴中自定义日期格式?

转载 作者:行者123 更新时间:2023-12-04 05:46:54 24 4
gpt4 key购买 nike

请原谅我这个基本问题。我在 R 中加载了一组数据作为 timeSeries。

> class(Return)

[1] "timeSeries"
attr(,"package")
[1] "timeSeries"

> head(Return[,1])

GMT
Overall
2005-09-21 1.8714
2005-09-22 0.2049
2005-09-23 -1.5924
2005-09-26 -4.3111
2005-09-27 -0.2416
2005-09-28 -1.1924

当我绘制这个时间序列数据时,它给了我一个日期作为 x 轴标签的图形,格式为 "2006-01-01" , "2007-01-01" .如何将其自定义为 "2006-01""2006""2006 Jan"以及如何修改频率?例如,我想每半年而不是每年打一次勾?

有什么建议吗?谢谢!

最佳答案

对于标签格式,您可以使用 format参数(有关格式选项的信息,请查看 this page ):

plot(Ts,format="%Y-%m") # 2006-01
plot(Ts,format="%Y-%b") # 2006-Jan
plot(Ts,format="%Y") # 2006

对于标签,您可以使用 at 设置自定义标签。参数,例如:
# compute the desired dates to show:
minDate <- timeCalendar(y=as.integer(format(min(time(Ts)),'%Y')),m=1,d=1)
maxDate <- max(time(Ts))
datesToShow = timeSequence(from=minDate,to=maxDate,by="1 year")

plot(Ts,format="%Y-%m",at=datesToShow)

有关 plot 的更多信息 timeSeries 的参数对象,只需键入:
?timeSeries::plot

关于r - 在 R 中的时间序列数据图的 X 轴中自定义日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10565267/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com