traffic_violations: Montgomery County Traffic Violations

Description Usage Format Details References Examples

Description

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

Usage

1

Format

A data frame with 1,276,580 rows and 35 variables

Details

References

https://data.montgomerycountymd.gov/Public-Safety/Traffic-Violations/4mse-ku6q

Examples

  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)

trinker/exampledata documentation built on May 25, 2019, 8:32 p.m.