[R] plot time series / dates (basic)

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Nov 1 22:58:10 CET 2004


On Mon, 1 Nov 2004, bogdan romocea wrote:

> Dear R users,
> 
> I'm having a hard time with some very simple things. I have a time
> series where the dates are in the format 7-Oct-04. 

So why use as.POSIXct for a date, rather than as.Date?

> I imported the
> file with read.csv so the date column is a factor. The series is
> rather long and I want to plot it piece by piece. The function below
> works fine, except that the labels for date are meaningless (ie
> 9.47e+08 or 1098000000 - apparently the number of seconds since
> whatever). I don't want to convert the data frame to a ts object
> because there are missing days and I don't want any interpolation.
> 
> 1. How do I replace the date labels with something like 'Mar04',
> instead of 9.47e+08 / 1098000000?

Just don't convert them to that format.  You set up

>       dat <- vector()

which is not a dates object.  If you use standard R indexing, it will
work. If you throw the class away, it will not.  Try

       dat <- date[(j-1)*points+1):(j*points)]

etc (no for loop required).

If you want a different format, see ?axis.Date

> 2. In the PDF file, the space between the two graphs printed pair by
> pair is fairly large. Can I remove/reduce the area that seems
> reserved for Title and X label so that, on a page, the space between
> the graph at the top and the one at the bottom is minimized?

There's a whole chapter on this in `An Introduction to R': have you read 
it?

> 3. Given the function below, I haven't discovered a way to have
> "vara" appear as the Title or Y label in graphs.
> main=as.character(vara) lists all the values of vara (which is a
> column from the data frame d). So, how can I use the name of a vector
> as title or label in a plot?

That's almost an FAQ.  Use deparse(substitute(vara))

> d <- ('data.csv', header = T, sep = ",", quote="", dec=".", 
> 	fill = T, skip=0)
> attach(d)
> #function to plot a long time series piece by piece
> pl <- function(vara, varb, points)
> 	{
> 	date <- as.POSIXct(strptime(as.character(Date), "%d-%b-%y"), tz =
> "GMT")
> 	pr1 <- vector(mode="numeric")
> 	pr2 <- vector(mode="numeric")
> 	dat <- vector()
> 	for (j in 1:(round(length(Vol)/points)+1)) #number of plots
> 		{
> 		for (i in ((j-1)*points+1):(j*points)) 
> 			{
> 			pr1[i-points*(j-1)] <- vara[i]
> 			pr2[i-points*(j-1)] <- varb[i]
> 			dat[i-points*(j-1)] <- date[i]
> 			}
> 		par(mfrow=c(2,1)) 
> 		plot(dat, pr1, type="b")
> 		plot(dat, pr2, type="b")
> 		}
> 	}
> 
> pdf("Rplots.pdf")
> pl(Vol, atr, 50)
> dev.off()


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list