-2

I am dealing with a dataset of crimes and want to determine the peak time for a crime. I have successfully created a vector of POSIXct types (e.g., "2017-01-01 00:00:00 EST") but can only map all the dates using hist.POSIXt.

To clarify, I would like to plot a histogram where the x-axis is a 24-hr period broken down into bins of 15 minutes, where I plot all of my data, regardless of the date on which the crime occurred.

I am using the dataset here: https://data.somervillema.gov/Public-Safety/Police-Selected-Criminal-Incidents/4jey-jqxb

My code to create the vector:

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

    date_reported<-as.POSIXct(strptime(df$dtreported,"%m/%d/%Y %r"))
Cilantro Ditrek
  • 1,047
  • 1
  • 14
  • 26

1 Answers1

1

Something like:

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

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205