How to format axes in R, year and months

I have the following dataset

1890 mar 0.4 1890 apr 0.8 1890 may 1.0 ... 1989 jan 0.2 1989 feb 0.4 1989 mar 0.5 

How can I draw a line graph in R with the x-axis of the year displayed every 5 years?

My problem is not so much in creating the plot as in showing only those years that I want and posting them at the beginning of this year. Therefore, I do not want to tick in April, but in January.

+4
source share
2 answers

This should help you:

 # create some data tmp <- seq(as.POSIXct("1890-03-01", tz="GMT"), as.POSIXct("1920-03-01", tz="GMT"), by="month") df <- data.frame(date=tmp, val=rnorm(length(tmp))) # plot data plot(df$date, df$val, xaxt="n") tickpos <- seq(as.POSIXct("1890-01-01", tz="GMT"), as.POSIXct("1920-01-01", tz="GMT"), by="5 years") axis.POSIXct(side=1, at=tickpos) 

alt text http://img704.imageshack.us/img704/9341/axis.png

+4
source

You get what rcs (right!) Suggested by default, using zoo as a graph with lines and the same axis:

 R> library(zoo) R> zdf <- zoo(df$val, order.by=df$date) R> plot(zdf) R> 

The help(plot.zoo) examples show that for indexing dates, it’s better, basically, which rcs showed you, but with additional formatting through, say,

 R> fmt <- "%Y-%m" ## year-mon R> txt <- format(index(zdf), fmt) R> plot(zdf, xaxt='n') R> axis(side=1, at=index(zdf), lab=txt) R> 

If you multiply at and lab , you will also get fewer ticks.

+2
source

All Articles