Search code examples
rggplot2tidyverselubridate

How to extract time from datetime and plot histogram


I have a data which contains datetime of occurrence of certain event for many years.

I have created a small sample tibble with datetime. Can someone please help me with the following -

  1. The time is in UTC, how can I convert it to EST

  2. How can I plot (ggplot) the histogram of occurrence with time of day.

library(tidyverse)

tbl <- tibble(datetime = c("2019-06-17 21:55:00 UTC", "2019-06-06 21:50:00 UTC", "2018-09-20 11:14:00 UTC", "2019-02-07 16:12:00 UTC",
"2020-01-30 16:54:00 UTC", "2019-06-03 21:26:00 UTC", "2018-08-17 13:03:00 UTC", "2019-09-06 18:55:00 UTC",
"2019-03-28 10:30:00 UTC", "2019-11-21 20:57:00 UTC", "2019-05-29 16:47:00 UTC", "2019-11-06 00:32:00 UTC",
"2018-04-26 20:18:00 UTC", "2019-09-11 19:21:00 UTC", "2019-10-04 13:24:00 UTC", "2018-12-11 23:39:00 UTC",
"2019-10-03 19:15:00 UTC", "2020-11-11 22:11:00 UTC", "2020-12-17 23:11:00 UTC", "2019-07-25 18:20:00 UTC"))

Solution

  • The following will convert the tibble to a dataframe with the times in EST and then create a histogram.

    library(tidyverse)
    
    tbl <- tibble(datetime = c("2019-06-17 21:55:00 UTC", "2019-06-06 21:50:00 UTC", "2018-09-20 11:14:00 UTC", "2019-02-07 16:12:00 UTC",
                               "2020-01-30 16:54:00 UTC", "2019-06-03 21:26:00 UTC", "2018-08-17 13:03:00 UTC", "2019-09-06 18:55:00 UTC",
                               "2019-03-28 10:30:00 UTC", "2019-11-21 20:57:00 UTC", "2019-05-29 16:47:00 UTC", "2019-11-06 00:32:00 UTC",
                               "2018-04-26 20:18:00 UTC", "2019-09-11 19:21:00 UTC", "2019-10-04 13:24:00 UTC", "2018-12-11 23:39:00 UTC",
                               "2019-10-03 19:15:00 UTC", "2020-11-11 22:11:00 UTC", "2020-12-17 23:11:00 UTC", "2019-07-25 18:20:00 UTC"))
    
    time <- as.POSIXct(tbl$datetime, "%F %T", tz="UTC")
    
    time <- data.frame(datetime=strptime(format(time,"%T", tz="America/New_York"), "%H:%M:%S"))
    
    
    ggplot(data=time, aes(x=datetime)) +
      stat_bin(bins = 24)
      geom_histogram()
    

    enter image description here