[R] plot time series / dates (basic)
bogdan romocea
br44114 at yahoo.com
Tue Nov 2 17:58:46 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
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)?
Regards,
b.
#function to plot a long time series piece by piece
pl <- function(vara, varb, points)
{
date <- as.Date(as.character(Date), "%d-%b-%y")
pr1 <- vector(mode="numeric")
pr2 <- vector(mode="numeric")
#dat <- vector()
dat <- date[(j-1)*points+1):(j*points)]
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]
#dat <- date[i]
}
par(mfrow=c(2,1), mai=c(0.4, 0.5, 0.3, 0.1), omi=c(0.2, 0, 0, 0),
cex.axis=0.7, cex=1.2, cex.main=0.7, pch="*")
plot(dat, pr1, main=deparse(substitute(vara)), type="o")
#axis.Date(1,dat,format="%b%y")
plot(dat, pr2, main=deparse(substitute(varb)), type="o")
}
}
--- Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:
> 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