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

2015-05-15 Thread Bob O'Hara
Thanks Thierry, Peter, Richard, Jim and Jeff for your help! In the end
I used Thierry's suggetion, in essence to add a day onto the sequences
that start after midnight, and this seems to work fine.

Bob
P.S. What I do during the day is my business, even if it mainly sems
to involve feeding frozen fruit  yoghurt to parrots...

On 14 May 2015 at 17:56, Jeff Newmiller jdnew...@dcn.davis.ca.us wrote:
 I might do it this way using Jim's sample data:

 epoch - as.POSIXct( 1970-01-01 ) # any date you like
 dta - data.frame( Timestamps = epoch
   + as.difftime( ifelse( Times = 5/24
, Times
, Times + 1 )
, units=days )
  , Thing=Thing
  )
 brks - epoch + as.difftime( seq( 5, 29, 1 ), units=hours )
 plot( Thing ~ Timestamps, dta, xaxt=n, xlim=c( min(brks), max(brks) ) )
 axis.POSIXct( 1, at=brks, format=%H:%M )

 or, using ggplot2 instead of base graphics:

 library(ggplot2)
 library(scales)
 ggplot( dta, aes( x=Timestamps, y=Thing ) ) +
 geom_point() +
 scale_x_datetime( breaks=brks
 , limits=c( min(brks), max(brks) )
 , labels=date_format(%H:%M) ) +
 theme( axis.text.x = element_text( angle = 90, hjust = 1 ) )


 On Thu, 14 May 2015, Jim Lemon wrote:

 Hi Bob,
 Given the other answers I may be off target, but perhaps this will help:

 # create two nights worth of data
 Times-strptime(

 paste(c(2015-05-13,2015-05-14),paste(rep(c(18:23,0:6),2),:30:00,sep=)),
 %Y-%m-%d %H:%M:%S)
 # telescope the two nights into repeated hours
 Hours-strptime(format(Times,%H:%M:%S),%H:%M:%S)
 # get a measure that can be checked for the correct output
 calls_per_hour-sample(10:100,length(Hours))
 # plot the repeated values - looks okay
 plot(Hours,calls_per_hour)
 # now calculate the mean values for each hourly measurement
 mean_calls_per_hour-by(calls_per_hour,as.character(Hours),mean)
 # plot the means, making sure that the orders match
 plot(sort(unique(Hours)),mean_calls_per_hour)

 Jim


 On Wed, May 13, 2015 at 1:20 AM, Richard M. Heiberger r...@temple.edu
 wrote:

 Try this.

 From the full data-time value subtract 18:00:00.

 This places the times you are interested in into the range 00:00:00 -
 12:00:00
 Remove the date from these adjusted date-time values and plot y
 against the new times.
 Take control of the tick-labels and display 18:00 - 0600 instead of
 the default 00:00 - 12:00

 Rich

 On Tue, May 12, 2015 at 10:34 AM, Bob O'Hara rni@gmail.com wrote:

 I'm helping colleagues with analysis of frog calls at night, 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?

 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@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.


 __
 R-help@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.


 __
 R-help@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.


 ---
 Jeff NewmillerThe .   .  Go Live...
 DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
 

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

2015-05-14 Thread Jeff Newmiller

I might do it this way using Jim's sample data:

epoch - as.POSIXct( 1970-01-01 ) # any date you like
dta - data.frame( Timestamps = epoch
  + as.difftime( ifelse( Times = 5/24
   , Times
   , Times + 1 )
   , units=days )
 , Thing=Thing
 )
brks - epoch + as.difftime( seq( 5, 29, 1 ), units=hours )
plot( Thing ~ Timestamps, dta, xaxt=n, xlim=c( min(brks), max(brks) ) )
axis.POSIXct( 1, at=brks, format=%H:%M )

or, using ggplot2 instead of base graphics:

library(ggplot2)
library(scales)
ggplot( dta, aes( x=Timestamps, y=Thing ) ) +
geom_point() +
scale_x_datetime( breaks=brks
, limits=c( min(brks), max(brks) )
, labels=date_format(%H:%M) ) +
theme( axis.text.x = element_text( angle = 90, hjust = 1 ) )

On Thu, 14 May 2015, Jim Lemon wrote:


Hi Bob,
Given the other answers I may be off target, but perhaps this will help:

# create two nights worth of data
Times-strptime(
paste(c(2015-05-13,2015-05-14),paste(rep(c(18:23,0:6),2),:30:00,sep=)),
%Y-%m-%d %H:%M:%S)
# telescope the two nights into repeated hours
Hours-strptime(format(Times,%H:%M:%S),%H:%M:%S)
# get a measure that can be checked for the correct output
calls_per_hour-sample(10:100,length(Hours))
# plot the repeated values - looks okay
plot(Hours,calls_per_hour)
# now calculate the mean values for each hourly measurement
mean_calls_per_hour-by(calls_per_hour,as.character(Hours),mean)
# plot the means, making sure that the orders match
plot(sort(unique(Hours)),mean_calls_per_hour)

Jim


On Wed, May 13, 2015 at 1:20 AM, Richard M. Heiberger r...@temple.edu wrote:

Try this.


From the full data-time value subtract 18:00:00.

This places the times you are interested in into the range 00:00:00 - 12:00:00
Remove the date from these adjusted date-time values and plot y
against the new times.
Take control of the tick-labels and display 18:00 - 0600 instead of
the default 00:00 - 12:00

Rich

On Tue, May 12, 2015 at 10:34 AM, Bob O'Hara rni@gmail.com wrote:

I'm helping colleagues with analysis of frog calls at night, 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?

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@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.


__
R-help@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.


__
R-help@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.



---
Jeff NewmillerThe .   .  Go Live...
DCN:jdnew...@dcn.davis.ca.usBasics: ##.#.   ##.#.  Live Go...
  Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/BatteriesO.O#.   #.O#.  with
/Software/Embedded Controllers)   .OO#.   .OO#.  rocks...1k

__
R-help@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, 

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

2015-05-13 Thread Jim Lemon
Hi Bob,
Given the other answers I may be off target, but perhaps this will help:

# create two nights worth of data
Times-strptime(
 paste(c(2015-05-13,2015-05-14),paste(rep(c(18:23,0:6),2),:30:00,sep=)),
 %Y-%m-%d %H:%M:%S)
# telescope the two nights into repeated hours
Hours-strptime(format(Times,%H:%M:%S),%H:%M:%S)
# get a measure that can be checked for the correct output
calls_per_hour-sample(10:100,length(Hours))
# plot the repeated values - looks okay
plot(Hours,calls_per_hour)
# now calculate the mean values for each hourly measurement
mean_calls_per_hour-by(calls_per_hour,as.character(Hours),mean)
# plot the means, making sure that the orders match
plot(sort(unique(Hours)),mean_calls_per_hour)

Jim


On Wed, May 13, 2015 at 1:20 AM, Richard M. Heiberger r...@temple.edu wrote:
 Try this.

 From the full data-time value subtract 18:00:00.
 This places the times you are interested in into the range 00:00:00 - 12:00:00
 Remove the date from these adjusted date-time values and plot y
 against the new times.
 Take control of the tick-labels and display 18:00 - 0600 instead of
 the default 00:00 - 12:00

 Rich

 On Tue, May 12, 2015 at 10:34 AM, Bob O'Hara rni@gmail.com wrote:
 I'm helping colleagues with analysis of frog calls at night, 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?

 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@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.

 __
 R-help@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.

__
R-help@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.


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

2015-05-12 Thread peter dalgaard

On 12 May 2015, at 16:34 , Bob O'Hara rni@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@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@cbs.dk  Priv: pda...@gmail.com

__
R-help@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.


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

2015-05-12 Thread Richard M. Heiberger
Try this.

From the full data-time value subtract 18:00:00.
This places the times you are interested in into the range 00:00:00 - 12:00:00
Remove the date from these adjusted date-time values and plot y
against the new times.
Take control of the tick-labels and display 18:00 - 0600 instead of
the default 00:00 - 12:00

Rich

On Tue, May 12, 2015 at 10:34 AM, Bob O'Hara rni@gmail.com wrote:
 I'm helping colleagues with analysis of frog calls at night, 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?

 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@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.

__
R-help@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.


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

2015-05-12 Thread Thierry Onkelinx
Dear Bob,

Is this useful?

library(lubridate)
library(ggplot2)
n - 100
h - sample(c(18:23, 0:9), size = n, replace = TRUE)
m - sample(0:59, size = n, replace = TRUE)
d - sample(1:3, size = n, replace = TRUE)
dataset - data.frame(
  Time = as.POSIXct(paste0(2015-01-, d,  , h, :, m, :0)),
  Thing = rnorm(n)
)
dataset$rTime - round_date(dataset$Time, unit = day)
dataset$Time2 - dataset$Time - dataset$rTime + min(dataset$rTime)
ggplot(dataset, aes(x = Time2, y = Thing, colour = factor(rTime))) +
geom_point()

Best regards,


ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature and
Forest
team Biometrie  Kwaliteitszorg / team Biometrics  Quality Assurance
Kliniekstraat 25
1070 Anderlecht
Belgium

To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey

2015-05-12 16:34 GMT+02:00 Bob O'Hara rni@gmail.com:

 I'm helping colleagues with analysis of frog calls at night, 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?

 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@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.


[[alternative HTML version deleted]]

__
R-help@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.