[R] Plotting times at night and getting plot limits correct

peter dalgaard pdalgd at gmail.com
Tue May 12 17:13:37 CEST 2015


On 12 May 2015, at 16:34 , Bob O'Hara <rni.boh at gmail.com> wrote:

> I'm helping colleagues with analysis of frog calls at night,

What do you do during the daytime then? 

> and they
> want to plot call statistics against time. This means we hit a
> problem: we want the x-axis to start at (say) 18:00 and end at (say)
> 06:00. I'm reluctant to use the date as well, because we have data
> from several dates, but only want to plot against time of day.
> 
> Here's some code to illustrate the problem (don't worry about the data
> being outside the range of the plot: this is only for illustration).
> 
> library(chron)
> Times <- chron(times.=paste(c(18:23,0:9),":30:00", sep=""))
> Thing <- rnorm(length(Times)) # just something for the y-axis
> 
> plot(Times,Thing) # x-axis wrong
> plot(Times,Thing, xlim=chron(times.=c("05:00:00", "18:00:00"))) # x-axis right
> plot(Times,Thing, xlim=chron(times.=c("18:00:00", "05:00:00"))) #
> would like this to work...
> 
> Can anyone suggest a solution?

It may be sheer luck, but this seems to work OK:

> Times <- as.POSIXct("1970-01-01 18:00", tz="UTC")+as.difftime(seq(0,12,.5),units="hours")
> Things<-rnorm(25)
> plot(Times,Things) 

I think the chron route is doomed because of the fundamental confusion between going backwards in time and crossing midnight. With a little diligence you should be able to shift dates to a common origin.

-pd

> 
> Bob
> 
> -- 
> Bob O'Hara
> 
> Biodiversity and Climate Research Centre
> Senckenberganlage 25
> D-60325 Frankfurt am Main,
> Germany
> 
> Tel: +49 69 798 40226
> Mobile: +49 1515 888 5440
> WWW:   http://www.bik-f.de/root/index.php?page_id=219
> Blog: http://occamstypewriter.org/boboh/
> Journal of Negative Results - EEB: www.jnr-eeb.org
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list