如何从日期时间中提取时间并绘制直方图

How to extract time from datetime and plot histogram

我有一个数据,其中包含多年特定事件发生的日期时间。

我创建了一个带有日期时间的小样本 tibble。有人可以帮我解决以下问题吗 -

  1. 时间是UTC,如何转换成EST

  2. 如何绘制 (ggplot) 随时间变化的直方图。

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"))

以下将把 tibble 转换为带有 EST 时间的数据帧,然后创建直方图。

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()