本文介绍了R-当我绘制xts&时如何更改日期格式?动物园的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am wondering how I can change date format.

我正在处理的代码如下:

The code I am working on is following:

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

此结果

我想更改日期格式

"%m %d %Y"

如图所示

"%b-%d-%Y"

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

So I tried following after searching some tips:

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()"做错了.

But this doesn't help, and doesn't even show any labeling on x-axis. I suppose that I might did something wrong with "axis.Date()".

有人可以帮助我吗?

推荐答案

使用xts,您可以直接使用major.format.

With xts, you can use major.format directly.

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

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

However, you should know that zoo plots are generally more flexible.

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)

这篇关于R-当我绘制xts&amp;时如何更改日期格式?动物园的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 05:18