gpt4 book ai didi

R - 绘制 xts 和 zoo 对象时如何更改日期格式?

转载 作者:行者123 更新时间:2023-12-02 09:29:55 41 4
gpt4 key购买 nike

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

我正在处理的代码如下:

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

这个结果

enter image description here

我想改变日期格式

"%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()”做错了什么。

谁能帮帮我?

最佳答案

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

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

enter image description here

但是,您应该知道 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)

enter image description here

关于R - 绘制 xts 和 zoo 对象时如何更改日期格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319504/

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