[R] Plotting Time against Date for time series data? (2)
Sander Oom
slist at oomvanlieshout.net
Mon May 17 21:14:47 CEST 2004
Almost but not quite:
Someone told me R would produce such high quality graphics, I would never
need a separate graphics package again. This does however mean that I need
to be able to draw the graph exactly the way I want it to look!
I have searched the web and help files extensively, but there are few
references on plotting time on an axis. The function presented here
(http://tolstoy.newcastle.edu.au/R/help/00a/1031.html) could be useful, but
I'm not sure how to implement it in my example. Anyway there should be a
simpler way.
The following code uses the example again. It does have hours on the
x-axis, but the tic marks are printed as: 0, 5, 10, 15, 20. Not a very
intuitive frequency of tic marks. How do I get R to plot tic marks at: 0,
4, 8, 12, 16, 20, 24 or 0, 2, 4..., 24? I want the tic marks to be fixed,
i.e. they should still go from 0-24 even when the range of the data is smaller.
dates <- c("02/27/92", "02/27/92", "01/14/92", "01/14/92", "03/28/92",
"03/28/92") #
times <- c("23:03:20", "10:29:56", "01:03:30", "13:03:30", "18:21:03",
"06:56:26") #
x <- paste(dates, times) #
DateTime <- strptime(x, "%m/%d/%y %H:%M:%S") #
Date <- trunc(DateTime, "day")
Time <- difftime(DateTime, Date, units="hours")
startMonth <- as.POSIXct(as.Date("01/01/1992", format="%d/%m/%Y"))
endMonth <- as.POSIXct(as.Date("01/05/1992", format="%d/%m/%Y"))
plot(x=Date, y=Time,
xlab= "Date (month/year)", ylab= "Time (hours)", xaxt="n", yaxt="n",
xlim=c(startMonth, endMonth),
ylim=c(0, 24)
) #
axis.POSIXct(1, at=seq(startMonth, endMonth, by="month"), format="%m/%y")
#r <- as.POSIXct(range(Time), "hours") #
clock24 <-
strptime(c("0","2","4","6","8","10","12","14","16","18","20","22","23"), "%H")
r <- as.POSIXct(round(range(clock24), "hours"))
axis.POSIXct(2, at=seq(r[1], r[2], by="hour"), format="%H") #
More suggestions welcome,
Sander
At 18:39 2004/05/17, you wrote:
>On Mon, 17 May 2004, Sander Oom wrote:
>
> > The plot is nearly there! Using the axis.POSIXct command I have got the
> > x-axis under control. However, the units for the y-axis (Time) are in
> > seconds by default (i.e. range is from 0 to 1440). I'm trying to plot
> hours
> > along the y-axis, without changing the units for the plot itself, but
> > without any luck.
>
>I got hours for your example, I believe, but it does depend on the data.
>
>Try
>
>Time <- difftime(Date, DateTime, units="hours")
>
>and treat them as numbers, not as POSIXct (they are not dates).
>
>
> >
> > The #'s were a quick solution to stop WinEdt reformatting my code
> > automatically, now I discovered I can switch to a different wrap mode.
> >
> > This is the code just now:
> >
> > >
> > > psDateTime <- function(DateTime, FileName){
> > + strFileName <- paste("Analysis\\Graphics\\time_date_", FileName, sep
> > = "")
> > + startMonth <- as.POSIXct(as.Date("01/09/2003", format="%d/%m/%Y"))
> > + endMonth <- as.POSIXct(as.Date("01/06/2004", format="%d/%m/%Y"))
> > + startTime <- as.POSIXct(as.Date("00:00:00", format="%H:%m:%s"))
> > + endTime <- as.POSIXct(as.Date("23:59:59", format="%H:%m:%s"))
>
>Just startTime <- 0, endTime <- 24 should do.
>
> > + Date <- trunc(DateTime, "day")
> > + Time <- DateTime - Date
> > + postscript(strFileName)
> > + plot(x=Date, y=Time,
> > + xlab= "Date (month/year)", ylab= "Time (hours)", xaxt="n",
> #yaxt="n",
> > + xlim=c(as.POSIXct(startMonth), as.POSIXct(endMonth)),
> > + ylim=c(as.POSIXct(startTime), as.POSIXct(endTime))
> > + )
> > + axis.POSIXct(1, at=seq(startMonth, endMonth, by="month"),
> format="%m/%y")
> > + axis.POSIXct(2, at=seq(startTime, endTime, by="hours"), format="%H")
> > + pstamp(pwd=FALSE, time=TRUE)
> > + dev.off()
> > + }
> > > psDateTime(datKruger1$DaTim, "datKruger1.eps")
> > Error in plot.window(xlim, ylim, log, asp, ...) :
> > need finite ylim values
> > >
> >
> > and when removing the ylim line from the above code, an error associated
> > with the 'axis.POSICct(2....)' line:
> >
> > > psDateTime(datKruger1$DaTim, "datKruger1.eps")
> > Error in if (to <= from) stop("`to' must be later than `from'") :
> > missing value where TRUE/FALSE needed
> > >
> >
> > Any help much appreciated,
> >
> > Sander.
> >
> > At 15:58 2004/05/17, you wrote:
> > >On Mon, 17 May 2004, Slist wrote:
> > >
> > > > I have a data set containing GPS fixes of animal locations. To
> check that
> > > > the GPS's are working properly, I would like to plot the time of
> the fixes
> > > > (y-axis) against the date of the fixes (x-axis). If all works well, the
> > > > plot should show four regular fixes per day. The x-axis should be
> labelled
> > > > with month/year (i.e. 11/04) and the y-axis by hour from 00 to 24.
> I would
> > > > like to control the x-axis limits.
> > > >
> > > > I have looked at several date and time related help pages, but get
> > > horribly
> > > > lost in all the terminology. The main challenge is to isolate date and
> > > time
> > > > from the date/time object for plotting (marked ???). Therefore, I would
> > > > like the following example code to work:
> > > >
> > > > >
> > > > >
> > > > dates <- c("02/27/92", "02/27/92", "01/14/92", "01/14/92", "03/28/92",
> > > "03/28/92") #
> > > > times <- c("23:03:20", "10:29:56", "01:03:30", "13:03:30", "18:21:03",
> > > "06:56:26") #
> > > > x <- paste(dates, times) #
> > > > DateTime <- strptime(x, "%m/%d/%y %H:%M:%S") #
> > >
> > >Why do you end lines with #? It is rather confusing.
> > >
> > >Date <- trunc(DateTime, "day")
> > >Time <- DateTime - Date
> > >plot(Date, Time)
> > >
> > >appears to do what you want (except the x axis labelling, which you can
> > >alter by a call to axis.POSIXct and x-axis limits, which need to be set
> > >via xlim as a POSIXct object.).
> > >
> > >--
> > >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
> >
> >
>
>--
>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