12

I am working with a data frame where one of the columns consists of POSIXct date-time values. I am trying to plot a histogram of these timestamps using ggplot2 but I'm having two issues:

  1. I don't know how to set the binwidth in geom_histogram(). I'd like to set each bin to a day or a week. I've tried providing a difftime object, but I get an error. I also tried binwidth=1 but R just hangs.

  2. How do I set the limits in scale_x_time()? The only way I could get it to work was by converting my POSIXct timestamps using as.Date().

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168
Dave
  • 197
  • 1
  • 6

1 Answers1

24
  1. The binwidth is measured in seconds, so to bin per week set binwidth=7*24*60*60.
  2. Limits can be given as a vector of 2 POSIXct objects.

An example:

y<-as.POSIXct('1970/01/01')+cumsum(rnorm(100,mean=24*60*60,sd=24*60*60))
p<-qplot(y,binwidth=7*24*60*60,fill=I('steelblue'),col=I('black'))
p<-p+scale_x_datetime(major="1 week",
                      minor="1 days",
                      format="%e/%m/%Y",
                      limits=c(as.POSIXct('1970/02/01'),
                               as.POSIXct('1970/03/31')))
print(p)
Jyotirmoy Bhattacharya
  • 9,317
  • 3
  • 29
  • 38
  • 2
    %d gives a leading zero while %e gives a leading space on single digit days. Not really sure which one looks better. – Jyotirmoy Bhattacharya Mar 09 '10 at 16:00
  • 1
    Thanks! My issue with the scale was due to my using scale_x_time() rather than scale_x_datetime(). – Dave Mar 09 '10 at 19:11
  • Thanks - in my case I didn't realize that the binwidth was in seconds when the data type is POSIXct. So when I was setting binwidth=1, the plot would never finish because it was too granular. – bobfet1 Apr 05 '13 at 06:55