【问题标题】:Customise date format in X-axis of a plot of timeSeries date in R?在 R 中自定义时间序列数据图的 X 轴上的日期格式?
【发布时间】:2012-05-20 20:54:30
【问题描述】:

请原谅我提出这个基本问题。我在 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" 以及如何修改频率?例如,我想每半年而不是每年一次?

有什么建议吗?谢谢!

【问题讨论】:

    标签: r plot


    【解决方案1】:

    对于标签格式,您可以使用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)
    

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

    ?timeSeries::plot
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-21
      • 1970-01-01
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2018-12-04
      相关资源
      最近更新 更多