Description Usage Format Details References Examples
A dataset containing traffic violation records from January 1, 2015 to December 31, 2017. Taken from: https://data.montgomerycountymd.gov/Public-Safety/Traffic-Violations/4mse-ku6q First read about this data set from: https://www.nytimes.com/2018/01/30/upshot/do-fast-and-furious-movies-cause-a-rise-in-speeding.html
1 |
A data frame with 1,276,580 rows and 35 variables
'Date Of Stop'. Date of the traffic violation.
'Time Of Stop'. Time of the traffic violation.
'Latitude'. Latitude location of the traffic violation.
'Longitude'. Longitude location of the traffic violation.
'Accident'. If traffic violation involved an accident.
'Belts'. If traffic violation involved a seat belt violation.
'Personal Injury'. If traffic violation involved Personal Injury.
'Property Damage'. If traffic violation involved Property Damage.
'Fatal'. If traffic violation involved a fatality.
'Commercial License'. If driver holds a Commercial Drivers License.
'HAZMAT'. If the traffic violation involved hazardous materials.
'Commercial Vehicle'. If the vehicle committing the traffic violation is a commercial vehicle.
'Work Zone'. If the traffic violation was in a work zone.
'State'. State issuing the vehicle registration.
'VehicleType'. Type of vehicle (Examples: Automobile, Station Wagon, Heavy Duty Truck, etc.)
'Year'. Year vehicle was made.
'Make'. Manufacturer of the vehicle (Examples: Ford, Chevy, Honda, Toyota, etc.)
'Model'. Model of the vehicle.
'Color'. Color of the vehicle.
'Violation Type'. Violation type. (Examples: Warning, Citation, SERO)
'Charge'. Numeric code for the specific charge.
'Contributed To Accident'. If the traffic violation was a contributing factor in an accident.
'Race'. Race of the driver. (Example: Asian, Black, White, Other, etc.)
'Gender'. Gender of the driver (F = Female, M = Male)
‘Driver City'. City of the driver’s home address.
‘Driver State'. State of the driver’s home address.
'Arrest Type'. Type of Arrest (A = Marked, B = Unmarked, etc.)
https://data.montgomerycountymd.gov/Public-Safety/Traffic-Violations/4mse-ku6q
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | ## Not run:
# https://www.nytimes.com/2018/01/30/upshot/do-fast-and-furious-movies-cause-a-rise-in-speeding.html
# https://data.montgomerycountymd.gov/Public-Safety/Traffic-Violations/4mse-ku6q
data(traffic_violations)
library(tidyverse)
library(chron)
library(numform)
library(ggrepel)
traffic_violations_clean <- traffic_violations %>%
mutate(
`Date Of Stop` = as.Date(`Date Of Stop`, format = '%m/%d/%Y'),
year_of_stop = format(`Time Of Stop`, format="%Y"),
hour_of_day = `Time Of Stop` %>%
strptime(format="%H:%M:%S") %>%
format(format = '%H') %>%
as.numeric(),
day_of_week = factor(weekdays(`Date Of Stop`), levels = numform::constant_weekdays)
) %>%
mutate_at(
vars(Accident:`Work Zone`, `Contributed To Accident`),
function(x) {
case_when(
x == 'No' ~ FALSE,
x == 'Yes' ~ TRUE,
TRUE ~ NA
)
}
) %>%
select(-Geolocation) %>%
extract(VehicleType, c('Vehicle_Type_Code', 'Vehicle_Type'), '(\\d+)\\s+-\\s+([A-M].+)\\s*')
traffic_violations_clean %>%
group_by(day_of_week, hour_of_day) %>%
summarize(Count = n()) %>%
ggplot(aes(hour_of_day, Count, group = 1)) +
geom_line() +
facet_wrap(~day_of_week, ncol = 2) +
scale_x_continuous(breaks = seq(0, 23, by = 2),
labels = function(x) numform::f_12_hour(x, format = "%I %p")
) +
scale_y_continuous(labels = f_denom)
traffic_violations_clean %>%
group_by(day_of_week, hour_of_day) %>%
summarize(Count = n()) %>%
ungroup() %>%
mutate(weekend = day_of_week %in% c('Sunday', 'Saturday', 'Friday')) %>%
ggplot(aes(hour_of_day, Count, group = day_of_week, color = day_of_week)) +
geom_line(aes(linetype = weekend)) +
scale_x_continuous(
limits = c(-2, 23),
breaks = seq(0, 23, by = 2),
labels = function(x) numform::f_12_hour(x, format = "%I %p")
) +
scale_y_continuous(labels = f_denom) +
geom_text_repel(
data = traffic_violations_clean %>%
count(day_of_week, hour_of_day) %>%
rename(Count = n) %>%
filter(hour_of_day == 0),
aes(label = day_of_week),
size = 4,
nudge_x = -1,
segment.color = 'grey80'
) +
theme(legend.position = 'none')
traffic_violations_clean %>%
group_by(day_of_week, hour_of_day) %>%
summarize(Count = n()) %>%
ggplot(aes(hour_of_day, Count, group = day_of_week, color = day_of_week)) +
geom_line() +
scale_x_continuous(breaks = seq(0, 23, by = 2),
labels = function(x) numform::f_12_hour(x, format = "%I %p")
) +
scale_y_continuous(labels = f_denom) +
coord_polar()
## Indicates this sample doesn't typically contain DUI/DWI
traffic_violations_clean %>%
filter(Alcohol) %>%
group_by(day_of_week, hour_of_day) %>%
summarize(Count = n()) %>%
ungroup() %>%
mutate(weekend = day_of_week %in% c('Sunday', 'Saturday', 'Friday')) %>%
ggplot(aes(hour_of_day, Count, group = day_of_week, color = day_of_week)) +
geom_line(aes(linetype = weekend)) +
scale_x_continuous(
limits = c(-2, 23),
breaks = seq(0, 23, by = 2),
labels = function(x) numform::f_12_hour(x, format = "%I %p")
) +
scale_y_continuous(labels = f_denom) +
geom_text_repel(
data = traffic_violations_clean %>%
filter(Alcohol) %>%
count(day_of_week, hour_of_day) %>%
rename(Count = n) %>%
filter(hour_of_day == 0),
aes(label = day_of_week),
size = 4,
nudge_x = -1,
segment.color = 'grey80'
) +
theme(legend.position = 'none')
## End(Not run)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.