[R] X axis label as months

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 22 00:10:26 CEST 2013


On Wed, Aug 21, 2013 at 5:56 PM, Prof J C Nash (U30A) <nashjc at uottawa.ca> wrote:
> There are several items on the web about putting month names as tick labels
> on x axis of time plots, but I found when I tried them they did not work for
> me. After an hour or so of driving myself silly looking for a bug in my
> code, I've prepared a reproducible example below, where I did find a
> workaround, but would prefer to be able to plot a ts object.
>
> Perhaps someone can spot the error in  this. I'm sure it's something silly
> or fumble-fingered.
>
> JN
>
> yy<-rnorm(28, 5, 1)
> yy
> yt<-ts(yy, frequency=12, start=c(2011, 1))
> yt
> plot(yt, xaxt="n", xlab="month")
> axis(1, at=1:28, label=month.abb) # fails  28 != 12
> axis(1, at=1:12, label=month.abb) # fails -- no axis labels
> X11()
> tt<-1:28
> plot(tt, yy, type='l', xaxt="n")
> axis(1, at=1:12, label=month.abb) # too short on axis
> axis(1, at=1:28, label=month.abb) # fails 28 != 12
> months<-c(month.abb, month.abb, month.abb[1:4])
> length(months)
> axis(1, at=1:28, label=months) # works! But why not the others?
>


In the zoo package see the example in ?plot.zoo labelled

## plot a mulitple ts series with nice x-axis using panel function


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list