3

我想知道如何更改日期格式。

我正在处理的代码如下:

library(quantmod)
getSymbols("AAPL")
price_AAPL <- AAPL[,6]
plot(price_AAPL, main = "The price of AAPL")

这个结果

在此处输入图像描述

我想更改日期格式

"%m %d %Y"

如图所示

"%b-%d-%Y"

所以我在搜索了一些提示后尝试了以下操作:

plot(price_AAPL, main = "The price of AAPL", xaxt="n")
axis.Date(1,
          at=seq(head(index(price_AAPL),1), 
                 tail(index(price_AAPL),1), length.out=5), 
          format="%b-%d-%Y", las=2)

但这无济于事,甚至没有在 x 轴上显示任何标签。我想我可能对“axis.Date()”做错了。

有谁能够帮助我?

4

1 回答 1

6

有了xts,就可以major.format直接使用了。

plot(price_AAPL, main = "The price of AAPL",major.format="%b-%d-%Y")

在此处输入图像描述

但是,您应该知道zoo绘图通常更灵活。

plot.zoo(price_AAPL, main = "The price of AAPL", xaxt="n", xlab="")
axis.Date(1,at=pretty(index(price_AAPL)),
            labels=format(pretty(index(price_AAPL)),format="%b-%d-%Y"),
            las=2, cex.axis=0.7)

在此处输入图像描述

于 2015-12-16T21:25:44.650 回答