Home > other >  How to handle 24 hours clock across days using ggplot2?
How to handle 24 hours clock across days using ggplot2?

Time:11-26

Code to generate data using tidyverse v1.3.2:

submits <- tibble::tribble(
  ~ id, ~ submission, ~ day,
  1, "18:25:12", 2,
  2, "01:37:40", 3,
  3, "11:40:50", 2, 
  4, "00:55:00", 2
)

Data:

# A tibble: 4 × 3
     id submission   day
  <dbl> <chr>      <dbl>
1     1 18:25:12       2
2     2 01:37:40       3
3     3 11:40:50       2
4     4 00:55:00       2

The column submission denotes when id submitted a form using a 24 hr clock with reference to a particular date - we can assume the date information is not available to us. The column day denotes the days passed between the reference date and the submission date.

I wish to plot a scatter plot with day on x-axis and submission on y-axis for all submission (no grouping, duplicates from a particular id is allowed).

However, I have been unable to accomplish it with the following code:

submits %>%
  mutate(submission = as.POSIXct(submission, format = "%H:%M:%S")) %>%
  ggplot(aes(x= day, y = submission))   
  geom_point()   
  scale_y_datetime(
    date_labels = "%H:%M", 
    limits = c(as.POSIXct("00:00:01", format="%H:%M:%S"), 
               as.POSIXct("23:59:59", format="%H:%M:%S")))

which plots:

enter image description here

However, the redundant 00:00 labels on y-axis is confusing.

How do I then plot an appropriate day vs. submission scatterplot while using y-axis as a 24 hrs clock?

CodePudding user response:

You could extract the hour minute second from your submission using hms from hms and use scale_y_time to make the y axis time like this:

library(lubridate)
library(dplyr)
library(ggplot2)
submits %>%
  mutate(submission = as.POSIXct(submission, format = "%H:%M:%S")) %>%
  mutate(submission = submission   lubridate::days(day - min(day))) %>%
  mutate(submission_time = hms::hms(second(submission),minute(submission),hour(submission))) %>%
  ggplot(aes(x= day, y = submission_time))   
  geom_point()   
  scale_y_time()

Created on 2022-11-26 with enter image description here

  • Related