[R] Plotting Time against Date for time series data?

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon May 17 15:58:54 CEST 2004


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




More information about the R-help mailing list