How to change binning on POSIXct histograms?

52 views
Skip to first unread message

Adam

unread,
Mar 30, 2009, 5:20:14 PM3/30/09
to ggplot2
Hi, I'm making a histogram of start times (POSIXct). For example,
qplot(startTime, data=df) . This chooses some reasonable bin width (a
few hours in my case).

But how can I explicitly control the binning? Like,
hist( df$startTime, breaks="30 mins")

The "breaks" argument to stat_bin doesn't seem to accept the time
break specification.

I've tried playing with the binwidth argument, but that seems to only
accept a number. I give it a number and R churns and churns and I have
to kill it. Not sure what that is doing, but whatever it is it's bad.

Is there a way to explicitly set the binning for a POSIXct histogram?

Thanks!
--- Adam

Adam

unread,
Mar 30, 2009, 5:44:45 PM3/30/09
to ggplot2
Well ok - I can do this...

> df$cuts <- cut(df$startTimeP, breaks="2 hours")
> dd <- as.data.frame.table(table(df$cuts))
> dd$p <- as.POSIXct( as.character(dd$Var1) )
> qplot(p, Freq, data=dd, geom="step")

But can I do this within histogram (which looks much nicer)?

Thanks again...

--- Adam

hadley wickham

unread,
Mar 30, 2009, 5:56:03 PM3/30/09
to Adam, ggplot2
On Mon, Mar 30, 2009 at 4:20 PM, Adam <ly...@fnal.gov> wrote:
>
> Hi, I'm making a histogram of start times (POSIXct). For example,
> qplot(startTime, data=df) . This chooses some reasonable bin width (a
> few hours in my case).
>
> But how can I explicitly control the binning? Like,
> hist( df$startTime, breaks="30 mins")
>
> The "breaks" argument to stat_bin doesn't seem to accept the time
> break specification.
>
> I've tried playing with the binwidth argument, but that seems to only
> accept a number. I give it a number and R churns and churns and I have
> to kill it. Not sure what that is doing, but whatever it is it's bad.
>

If you specify a binwidth, I'm pretty sure it will be in seconds.

Hadley

--
http://had.co.nz/

Adam

unread,
Mar 31, 2009, 3:47:49 AM3/31/09
to ggplot2
Indeed that works great. Don't know why I missed that sensible
feature. Perhaps you can add that to the documentation.

Thanks for the quick reply!
--- Adam
Reply all
Reply to author
Forward
0 new messages