This is a good question and highlights some of the difficulties with working with dates in R. The lubridate package is very convenient, so below I present two approaches, one of which uses the database (as suggested by @ RJ-), and the other using lubridate.
Restore (the first two lines) of the data block in the original message:
foo <- data.frame(start.time = c("2012-02-06 15:47:00", "2012-02-06 15:02:00", "2012-02-22 10:08:00"), duration = c(1,2,3))
Convert to POSIXct and POSIXt class (two ways to do this)
# using base::strptime t.str <- strptime(foo$start.time, "%Y-%m-%d %H:%M:%S")
Now choose the time in decimal hours
# using base::format h.str <- as.numeric(format(t.str, "%H")) + as.numeric(format(t.str, "%M"))/60
Demonstrate that these approaches are equal:
identical(h.str, h.lub)
Then choose one of the following approaches to set the decimal hour of foo$hr :
foo$hr <- h.str
then build using the ggplot2 package:
library(ggplot2) qplot(foo$hr, foo$duration) + scale_x_datetime(labels = "%S:00")
David LeBauer May 22 '12 at 18:42 2012-05-22 18:42
source share