[R] plot time series / dates (basic)

Gabor Grothendieck ggrothendieck at myway.com
Tue Nov 2 19:08:22 CET 2004

> Thank you for the suggestions. I managed to fix everything except the
: first part. 
: 	dat <- date[(j-1)*points+1):(j*points)]
: causes a syntax error. If I do 

You have unbalanced parentheses.

: 	dat <- vector() 
: I end up with numbers (which is fine by me - just like SAS dates).
: However, after checking a couple of sources I still have no idea how
: to format numbers as dates (for plotting/printing). Does anyone have
: an example for formatting 12710 (# of days since 1 Jan 1970) as
: 19-Oct-04 (in the x axis of a plot)?

You can use chron or Date classes:

	dd <- chron(12710:12721, out.format = "dd-mmm-yy")
	plot(dd, 1:12)
	as.Date.integer <- function(x) structure(x, class = "Date")
	dd <- as.Date(12710:12721)
	plot(dd, 1:12)

If you don't like that labelling you can use axis to set up
your own.  Continuing the last example:

	plot(dd, 1:12, xaxt = "n")
	axis.Date(1, dd, format = "%d-%b-%y", cex.axis = .5)

see ?axis, ?strptime and the Help Desk article in R News 4/1.

More information about the R-help mailing list