在一天中用 POSIXct 绘制时间

Plotting times in POSIXct over course of a day

我正在处理犯罪数据集,想确定犯罪的高峰时间。我已经成功创建了一个 POSIXct 类型的向量(例如,“2017-01-01 00:00:00 EST”)但只能使用 hist.POSIXt.

映射所有日期

为了澄清,我想绘制一个直方图,其中 x 轴是 24 小时的时间段,分为 15 分钟的区间,我在其中绘制了我的所有数据,而不管犯罪发生的日期发生了。

我在这里使用数据集:https://data.somervillema.gov/Public-Safety/Police-Selected-Criminal-Incidents/4jey-jqxb

我创建向量的代码:

    df<-read.csv("Police_-_Selected_Criminal_Incidents.csv", stringsAsFactors = FALSE)

    date_reported<-as.POSIXct(strptime(df$dtreported,"%m/%d/%Y %r"))

类似于:

library(RSocrata)
library(hrbrthemes) # devtools::install_github("hrbrmstr/hrbrthemes")
library(lubridate)
library(tidyverse)

events <- read.socrata("https://data.somervillema.gov/resource/6jia-qk6r.json")

tbl_df(events) %>% 
  mutate(hour=hour(dtreported), min=minute(dtreported), tick=(hour*60)+min) %>% 
  ggplot(aes(tick)) +
  geom_histogram(bins = 4*24, color="white", size=0.1) +
  scale_x_continuous(expand=c(0,1),
                     breaks=seq(0, 1439, length.out = 5),
                     labels=c("Midnight", "06:00", "Noon", "18:00", "23:59")) +
  scale_y_continuous(expand=c(0,0), limits=c(0, 250)) +
  labs(x=NULL, y="# Events", 
       title="Police - Selected Criminal IncidentsPublic Safety",
       subtitle="Robbery, burglary, theft of motor vehicle & theft from motor vehicle since 2005",
       caption="Source: <https://data.somervillema.gov/Public-Safety/Police-Selected-Criminal-Incidents/4jey-jqxb>") +
  theme_ipsum_rc(grid="Y") +
  theme(axis.text.x=element_text(hjust=c(0, 0.5, 0.5, 0.5, 1)))