我正在尝试在最后几个小时获得所需日期的刻度,标签为月 - 年,在时间序列图的x轴上 . 我已经在Stack和其他方面尝试了大量的东西,但到目前为止还没有成功 .

下面是我到目前为止所尝试的一个例子 . 我正在使用x轴作为数字(例如2014.0),而我希望它采用日期格式,例如2014年1月 .

我也在尝试学习,如果是数字年份标签,我怎么能用2014.1而不是2014.0开始我的x轴,因为我系列的第一个月是1月 . 为了得到这个,我尝试了offset = 1的时间函数,但它也没有用 . 请查看以下示例

## dataframe and its time series
temp_df<- data.frame(date_temp= as.Date(dates_of_data), opp_temp= rnorm(29,mean = 100,sd=5))
temp_ts<- ts(temp_df[,2], start=2014, freq=12)

## plot
plot(temp_ts, axes=F, lwd=3, ylim=c(min(temp_ts),max(temp_ts)), xlab="", ylab="",type="l",col="black", main="")
points(temp_ts,pch=20,col="yellow")

## y-axis
axis(2, ylim=c(min(temp_ts),max(temp_ts)),col="black",lwd=2,line=1)
mtext(2,text="Y-axis count",line=3,col="black")

## x-axis
axis(1,pretty(range(time(temp_ts)),12))
mtext("Time - Year",side=1,col="black",line=2, lwd=3)
   ## axis(1,pretty(range(time(temp_ts, offset=1)),12))  -- didnt work either


temp_dates<- as.Date(as.yearmon(time(temp_ts))) 

axis(side=1, at=tt, labels = FALSE)
axis(side = 1, at = tt[ix], labels = labs[ix], tcl = -0.7, cex.axis = 1)

grid (NULL,NULL, lty = 6, col = "blue") 
dev.off()

output of above

到目前为止我尝试过的几件事包括

# 1.  par(xaxt="n")  ## didn't work
# 2.  axis(1, at=seq(from = min(temp_dates),to = max(temp_dates), by="month"), labels=format(temp_dates,"%Y-%b"),las=2)

你能告诉我如何在2014年1月(月 - 月)获得x轴标签吗?

这些是我按要求完成的一些链接:here here

here herehere

EDIT :以下解决方案使用动物园库完美运行 . 但是我在学习期间并没有一直使用动物园,而是更喜欢用另一种方式 . 请更正 previous 方法中的错误 .

require(zoo)
dev.off()
x<- (zoo(temp_df$opp_temp, temp_df$date_temp))
plot(x, xaxt = "n")
x_times <- time(x)
ticks <- seq(x_times[1], x_times[length(x_times)], by = "month")
grid (5,5, lty = 6, col = "blue") 
axis(1, at = ticks, labels = format(x_times,"%Y-%b"),las=2, tcl = -0.3)