Traffic Stop Reason-Outcome Chart

Select the reason for the traffic stop and see the percent of stops by race. Then, select the race to see the outcome of the stop by race. (Blue circles are expandable, White circles are not)


  • Race is observed by the police officer.
  • Drivers of Unknown race have been removed.
  • White and Black races include only non-Hispanic drivers.
  • Hispanic is listed as its own race category.
  • The values represent the percent of stops by reason and race

  • Infraction = Ticket
  • Warning = Verbal or Written
  • Other includes: No Disposition, Misdemeanor Summons, Uniform Arrest Report, and Unknown
    ---
    title: "Connecticut Traffic Stops"
    output: 
      flexdashboard::flex_dashboard:
        storyboard: true
        author: Jenna Daly
        source_code: embed
        social: menu
        vertical_layout: fill
    ---
    
    
    
    ```{r setup, include=FALSE}
    library(flexdashboard)
    library(stringr)
    library(dplyr)
    
    reason_outcome_by_race <- complete_df_orgs_clean %>% 
      select(`Federal Fiscal Year`, SubjectRaceCode, SubjectEthnicityCode, StatutoryReasonForStop, InterventionDispositionCode)
    
    reason_outcome_by_race$SubjectRaceCode[reason_outcome_by_race$SubjectEthnicityCode == "Hispanic"] <- "Hispanic"
    
    reason_outcome_by_race$SubjectEthnicityCode <- NULL
    
    #recode outcome to combine 
    reason_outcome_by_race$Outcome <- "Other"
    reason_outcome_by_race$Outcome[grep("Warning", reason_outcome_by_race$InterventionDispositionCode)] <- "Warning"
    reason_outcome_by_race$Outcome[grep("Infraction", reason_outcome_by_race$InterventionDispositionCode)] <- "Infraction"
    
    reason_outcome_by_race$InterventionDispositionCode <- NULL
    
    reason_outcome_by_race$count <- 1
    num_race<- aggregate(count~.,reason_outcome_by_race,sum)
    
    options(scipen=9999)
    library(dplyr)
    #detach(package:plyr)
    num_race_agg <- num_race %>% 
      group_by(`Federal Fiscal Year`, SubjectRaceCode) %>% 
      mutate(`Total For Each Race` = sum(count))
    
    num_race_agg <- num_race_agg %>% 
      group_by(`Federal Fiscal Year`, SubjectRaceCode, StatutoryReasonForStop) %>% 
      mutate(`Total For Each Reason` = sum(count))
    
    num_race_agg <- num_race_agg %>% 
      group_by(`Federal Fiscal Year`, StatutoryReasonForStop) %>% 
      mutate(`All Races For Each Reason` = sum(count))
    
    num_race_calc <- num_race_agg %>% 
      mutate(`Reason %` = (`Total For Each Reason` / `Total For Each Race`)*100, 
             `Outcome %` = (`count` / `Total For Each Reason`)*100, 
             `Race %` =  (`Total For Each Reason` / `All Races For Each Reason`)*100)
    
    write.table(
      num_race_calc,
      file.path(getwd(), "data", "data_for_reason_outcome_flow_diagram3.csv"),
      sep = ",",
      row.names = F
    )
    
    path_to_data <- paste0(getwd(), "/", "data")
    raw_data <- dir(path_to_data,  pattern = "3.csv")
    num_race_calc <- read.csv(paste0(path_to_data, "/", raw_data), stringsAsFactors=FALSE, header=T, check.names=FALSE)
    
    ```
    
    Traffic Stop Reason-Outcome Chart {.storyboard}
    ==================================================
    
    ### Select the reason for the traffic stop and see the percent of stops by race. Then, select the race to see the outcome of the stop by race. (Blue circles are expandable, White circles are not) {data-commentary-width=250}
    
    ```{r}
    library(collapsibleTree)
    selected_reasons <- c("Speed Related", "STC Violation", "Cell Phone", "Defective Lights")
    
    num_race_calc_for_chart <- num_race_calc[num_race_calc$`Federal Fiscal Year` == "2015-2016" & num_race_calc$SubjectRaceCode != "Unknown" & num_race_calc$StatutoryReasonForStop %in% selected_reasons,]
    
    num_race_calc_for_chart$`Reason %` <- round(num_race_calc_for_chart$`Reason %`, 0)
    num_race_calc_for_chart$`Reason %` <- gsub("$", "%", num_race_calc_for_chart$`Reason %`)
    
    num_race_calc_for_chart$`Outcome %` <- round(num_race_calc_for_chart$`Outcome %`, 0)
    num_race_calc_for_chart$`Outcome %` <- gsub("$", "%", num_race_calc_for_chart$`Outcome %`)
    
    num_race_calc_for_chart$`Race %` <- round(num_race_calc_for_chart$`Race %`, 0)
    num_race_calc_for_chart$`Race %` <- gsub("$", "%", num_race_calc_for_chart$`Race %`)
    
    num_race_calc_for_chart$SubjectRaceCode <- factor(num_race_calc_for_chart$SubjectRaceCode, 
                                       levels= c("White", "Black", "Hispanic", 
                                                 "Asian", "American Indian"))
    
    num_race_calc_for_chart$StatutoryReasonForStop <- factor(num_race_calc_for_chart$StatutoryReasonForStop, 
                                       levels= selected_reasons)
    
    num_race_calc_for_chart$Outcome <- factor(num_race_calc_for_chart$Outcome, 
                                       levels= c("Infraction", "Warning", "Other"))
    
    num_race_calc_for_chart <- num_race_calc_for_chart %>% arrange(StatutoryReasonForStop, SubjectRaceCode, Outcome)
    
    num_race_calc_for_chart$`Race and %` <- paste(num_race_calc_for_chart$SubjectRaceCode, ": ", num_race_calc_for_chart$`Race %`)
    
    collapsibleTree(num_race_calc_for_chart, root = "Choose a Reason", zoomable = T, fillByLevel = F,  
                    tooltip=F, fontSize = 16, collapsed=T, fill = "steelblue",
                    hierarchy = c("StatutoryReasonForStop", "Race and %", "Outcome", "Outcome %"))
    ```
    
    ***