格式化时间序列图的x轴作为日期

时间:2017-02-09 09:14:30

标签: r plot

如何在x轴上添加日期而不是十进制数?

dates<-seq(from=as.Date("2000/7/1"), by="month", length.out=18)
y<-rnorm(18,20,3)
myts<-ts(data=y,
         start=c(as.numeric(format(min(dates),"%Y")),
                 as.numeric(format(min(dates),"%m"))),
         frequency=12,
         deltat=1/12)
plot(myts,ylab='Y',xlab='Date',type='l')

谢谢和亲切的问候

2 个答案:

答案 0 :(得分:1)

也许只绘制而不转换为时间序列,在绘制时用xaxt = "n"抑制x轴标签,然后稍后用axis添加x轴标签。

dates_label = as.character(dates)
plot(x = dates, y, las = 2, xaxt = "n", xlab = "", type = "l")
axis(1, at = dates, labels = dates_label, las = 2, cex.axis = .85)

enter image description here

答案 1 :(得分:0)

格式化x轴的最简单方法可能是用ggplot绘图:

inverse_transform()

enter image description here

相关问题