Creating ggplot2 Histogram with Times Around Midnight

289 views Asked by At

I have a set of data that is the end time of two industrial processes. These processes end around midnight every night and I am trying to visualize their performance next to each other by plotting a dodged histogram of the times that they end. Unfortunately, I don't know how to split the data in a way that ggplot will use it in a contiguous histogram. Here is a sample of the data that I am using:

testset <- data.table(ProcessA=as.POSIXct(c("2015-01-01 22:12:00",
                          "2015-01-02 22:53:00","2015-01-03 23:42:00",
                          "2015-01-05 00:02:00","2015-01-05 23:33:00",
                          "2015-01-06 23:56:00","2015-01-08 00:19:00",
                          "2015-01-08 23:29:00","2015-01-09 23:14:00",
                          "2015-01-11 00:01:00")), 
                       ProcessB=as.POSIXct(c("2015-01-01 22:12:00",
                          "2015-01-02 23:35:00","2015-01-03 23:18:00",
                          "2015-01-05 00:09:00","2015-01-05 23:03:00",
                          "2015-01-06 22:51:00","2015-01-08 00:22:00",
                          "2015-01-09 00:12:00","2015-01-09 23:01:00",
                          "2015-01-11 00:23:00")))

I have subsetted to get only the times by using:

testset$ProcessAtime <- substr(testset$ProcessA, 12, 16)
testset$ProcessAtime <- substr(testset$ProcessA, 12, 16)

However these give me things I can't plot and still stay around midnight. I've tried zoo but don't see a way to do what I'm asking to do. I have previously done things like this (see here and here) but nothing that allows me to use times to create plots such as the one I would like to create.

Any help would be super greatly appreciated!

1

There are 1 answers

4
MrFlick On BEST ANSWER

Base R doesn't have a time only date format do you need a date. You can set them all to the same date, or if you want times near 0 to be near times near 24, you might split them across two dates. Here's an example of the latter

justtime <- function(x, split=12) {
    h <- as.numeric(strftime(x, "%H"))
    y <- as.POSIXct(paste(ifelse(h<split, "2015-01-02","2015-01-01"),strftime(x, "%H:%M:%S")))
}

Then you can plot with

ggplot(data.frame(time=justtime(testset$ProcessA)), aes(x=time)) + 
    geom_histogram()

which gives

enter image description here