在一天中的POSIXct中绘制时间

时间:2017-02-24 22:28:13

标签: r posixct

我正在处理犯罪数据集,并希望确定犯罪的高峰时间。我已经成功创建了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"))

1 个答案:

答案 0 :(得分:1)

类似的东西:

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

enter image description here