[R] X axis label as months

Jim Lemon jim at bitwrit.com.au
Thu Aug 22 02:51:08 CEST 2013


On 08/22/2013 09:04 AM, Prof J C Nash (U30A) wrote:
> Thanks. The staxlab works with the plot generated by
>
> plot(tt, data,....)
>
> but not with
>
> plot(tsdata, ...)
>
> i.e., it seems to be the tsplot that somehow changes something and the
> axis commands have no effect.
>
> I probably should have pointed out that my main concern is that I'm not
> getting any error msg. Just no axis label. And it seems I need to make
> sure the labels vector is the same length as I need -- no recycling.
>
> JN

Hi JN,
This is an interesting problem for me as it affords an opportunity to 
learn about time series. I can hack a quick solution like this:

plotlim<-par("usr")
xlim<-c(plotlim[1]+(plotlim[2]-plotlim[1])*0.04,plotlim[2]-
  (plotlim[2]-plotlim[1])*0.04)
xlim
[1] 2011.007 2013.243
nint<-length(yt)-1
labpos<-cumsum(c(xlim[1],rep(diff(xlim)/nint,nint)))
staxlab(1, at=labpos, label=months)

This works, but it is not a very elegant way to get the x positions of a 
time series. It does allow one to calculate the x positions of any 
equally spaced series of plotted values (using par("xaxt") to work out 
the space at the edges).

The "ts" function seems to infer that a "start=c(2011,1),frequency=12" 
time series refers to years and months, but assigns the x positions as 
decimal years. I cannot find a way to extract the x positions from the 
time series and could only offer a really ugly function that tried to 
guess the intervals using the "start" and "frequency" arguments and then 
calculated the x positions from these. However, I can see that this 
would be a useful thing to have, so I'll think about it.

Jim



More information about the R-help mailing list