[R] X axis label as months

Prof J C Nash (U30A) nashjc at uottawa.ca
Wed Aug 21 23:56:17 CEST 2013


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?

##  > sessionInfo()
##  R version 3.0.1 (2013-05-16)
##  Platform: x86_64-pc-linux-gnu (64-bit)
##
##  locale:
##   [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C
##   [3] LC_TIME=en_CA.UTF-8        LC_COLLATE=en_CA.UTF-8
##   [5] LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8
##   [7] LC_PAPER=C                 LC_NAME=C
##   [9] LC_ADDRESS=C               LC_TELEPHONE=C
##  [11] LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C
##
##  attached base packages:
##  [1] stats     graphics  grDevices utils     datasets  methods   base 

##
##  loaded via a namespace (and not attached):
##  [1] tools_3.0.1
##  >



More information about the R-help mailing list