The Fatality Analysis Reporting System (FARS) contains data derived from a census of fatal traffic crashes within the 50 States


FARS was conceived, designed, and developed by the National Center for Statistics and Analysis (NCSA) of the National Highway Traffic Safety Administration in 1975:

The FARSread package was created to make summary and visualization of the FARS data user-friendly in R


The FARSread package has three functions:

In 2015 the the states with the highest number of accidents were Texas, California, and Florida


The fars_map_state function makes it easy to visually illustrate the number of accidents in a particular state over years


The fars_map_state allows for some features to be added on the map plot to improve readability by adding a few more information.


By providing features like:

we can add emphasis and improve how we convey information to our listeners during presentations.

We can borrow from the package leaflet some functions to creatre an interactive plot


An interactive map provides us with the flexibility to move the map around and to zoom in or out for greater details.

---
title: "The FARSread Package"
output: 
  flexdashboard::flex_dashboard:
    storyboard: true
    social: menu
    source: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(FARSread)
library(dplyr)
library(tidyr)
library(ggplot2)
library(plotly)
library(leaflet)
```

### The Fatality Analysis Reporting System (FARS) contains data derived from a census of fatal traffic crashes within the 50 States


```{r}
comp_yrs <- fars_summarize_years(c(2013, 2014, 2015))
names(comp_yrs) <- c("month", "yr2013", "yr2014", "yr2015")
comp_yrs <- comp_yrs %>% mutate(month = as.factor(month.abb))
comp_yrs$month <- factor(comp_yrs$month, levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
long_comp_yrs <- gather(comp_yrs, key = key, value = value, -month)
p <- ggplot(data = long_comp_yrs, aes(x = month, y = value, group = key, color = key)) + ### input the variables we want to plot
        geom_line(linetype = "dashed", size = 1) + ### show a line plot
        geom_point(size = 2, shape = 21) + ### show the points as well
        xlab("Month") + ### put a label on the x axis 
        ggtitle("Number of Accidents Recorded per Month 2013-2015") ### Put a title
ggplotly(p)
```

***

FARS was conceived, designed, and developed by the National Center for Statistics and Analysis (NCSA) of the National Highway Traffic Safety Administration in 1975:

- to provide an overall measure of highway safety, 

- to help identify traffic safety problems, 

- to suggest solutions, and 

- to help provide an objective basis to evaluate the effectiveness of motor vehicle safety standards and highway safety programs.

### The FARSread package was created to make summary and visualization of the  FARS data user-friendly in R

```{r}
accident_2015 <- fars_read("accident_2015.csv.bz2")
most_count <- accident_2015 %>% ###fars data object in R
        group_by(STATE) %>% ### groups the data according to State
        summarize(count = n()) %>% ### count the number of observations per State 
        arrange(desc(count)) ### arrange from highest to lowest
plot_most_count <- most_count %>% ### The table above we just created
                        slice(1:15) %>% ### Get only top 15 States
                        mutate(STATE = c("TX", "CA", "FL", "GA", "NC", "PA","NY",
                                         "OH","IL", "SD", "MI", "TN", "AR", "MS",
                                         "AL")) %>% ### Change to State abb Names
                        as.data.frame() ### save as a data frame
plot_most_count$STATE <- factor(plot_most_count$STATE, levels = c("TX", "CA", "FL", "GA", "NC", "PA", "NY", "OH", "IL", "SD", "MI", "TN", "AR", "MS", "AL"))
g <- ggplot(plot_most_count, aes(STATE)) + 
        geom_bar(aes(weight = count, fill = STATE)) +
        ggtitle("Top 15 States With Highest Number of Traffic Accidents in 2015")
ggplotly(g)
```

***

The FARSread package has three functions:

- The fars_read function for reading the data in R

- The fars_summarize_years function for comparing monthly summaries

- The fars_map_state function to create a map plot where the accidents occurred


### In 2015 the the states with the highest number of accidents were Texas, California, and Florida

```{r}
par(mfrow = c(1,3)) ### plot the map in three columns in a single row
par(oma = c(2, 0,0,0)) ### specify the margins to allow for titles and texts
par(mar=c(1,0.5,0.5,0.5)) ### specify the margins to fit the plot
fars_map_state(48, ### selects the number 48 which stands for the state of Texas
               2015) ### selects the year 2015
title("The State\n of\n Texas", ###text for title
      line=-2, ### position of the title
      cex.main = 1, ### size of the text
      adj = 0.8, ### left to right position of title
      col.main= "blue") ### color of the text

fars_map_state(6, ### selects the number 6 which stands for the state of California
               2015) ### selects the year 2015
title("The State of\n California", ###text for title
      line=-6, ### position of the title
      cex.main = 1, ### size of the text
      adj = 0.9, ### left to right position of title
      col.main= "blue") ### color of the text
mtext("States With Highest No.of Traffic Accidents-2015", ### title to print
      side = 1, ### print title on the bottom
      line = 0, ### start printin at line 0
      outer = TRUE, ### Use margins if available
      cex = 1.5) ### size of the text

fars_map_state(12, ### selects the number 12 which stands for the state of Florida
               2015) ### selects the year 2015
title("The\n State\n of\n Florida", ###text for title
      line=-8, ### position of the title
      adj = 0.4, ### left to right position of title
      cex.main = 1, ### size of the text
      col.main= "blue") ### color of the text
```

***

- Every dot on the map is a representation of where a traffic accident occured based on the latitude and longitudinal position reported in FARS data. 

- The confluence of dots in certain areas indicate the location where traffic  happened the most.

### The fars_map_state function makes it easy to visually illustrate the number of accidents in a particular state over years

```{r}
par(mfrow = c(1,3)) ### plot the map in three columns in a single row
par(oma = c(2, 0,0,0)) ### specify the margins to allow for titles and texts
par(mar=c(1,0.5,0.5,0.5)) ### specify the margins to fit the plot
fars_map_state(13, ### selects the number 48 which stands for the state of Texas
               2013) ### selects the year 2015
title("The State\n of\n Georgia\n 2013", ###text for title
      line=-4, ### position of the title
      cex.main = 1, ### size of the text
      adj = 0.95, ### left to right position of title
      col.main= "firebrick3") ### color of the text

fars_map_state(13, ### selects the number 48 which stands for the state of Texas
               2014) ### selects the year 2015
title("The State\n of\n Georgia\n 2014", ###text for title
      line=-4, ### position of the title
      cex.main = 1, ### size of the text
      adj = 0.95, ### left to right position of title
      col.main= "firebrick3") ### color of the text
mtext("Map Plot of Traffic Accidents in Georgia from 2013-2015", ### title to print
      side = 1, ### print title on the bottom
      line = 0, ### start printin at line 0
      outer = TRUE, ### Use margins if available
      cex = 1.5) ### size of the text

fars_map_state(13, ### selects the number 48 which stands for the state of Texas
               2015) ### selects the year 2015
title("The State\n of\n Georgia\n 2015", ###text for title
      line=-4, ### position of the title
      cex.main = 1, ### size of the text
      adj = 0.95, ### left to right position of title
      col.main= "firebrick3") ### color of the text
```

***

- Compared to numbers or tables, plots have the advantage of conveying more information at a glance.

- It also shows the the increase did not happen just in selected areas, but rather appears to have occured throughout the State.

### The fars_map_state allows for some features to be added on the map plot to improve readability by adding a few more information.

```{r}
par(oma = c(1, 0,0,0)) ### specify the margins to allow for titles and texts
par(mar=c(0.5,0.5,0.5,0.5))
fars_map_state(48, ### selects the number 48 which stands for the state of Texas
               2015) ### selects the year 2015
points(x = -95.38142, y =  29.80174, col = "red", cex = 1.7, pch = 0, lwd = 2)
text(x = -95.38142, y =  29.80174, labels = "201", col = "red", cex = 1.2, pos = 1, offset = 1.5)
points(x = -96.79868, y =  32.80453, col = "blue", cex = 1.7, pch = 1, lwd = 2)
text(x = -96.79868, y =  32.80453, labels = "113", col = "blue", cex = 1.2, pos = 3, offset = 1.5)
title("The State\n of\n Texas", ###text for title
      line=-2, ### position of the title
      cex.main = 1, ### size of the text
      adj = 0.8, ### left to right position of title
      col.main= "black") ### color of the text
mtext("Map Plot of Traffic Accidents Using Geospatial Locationing", ### title to print
      side = 1, ### print title on the bottom
      line = 0, ### start printin at line 0
      outer = TRUE, ### Use margins if available
      cex = 1) ### size of the text
```

***

By providing features like:

- circles,

- squares, 

- dots or even 

- arrows, 

we can add emphasis and improve how we convey information to our listeners during presentations.

### We can borrow from the package leaflet some functions to creatre an interactive plot

```{r}
leaflet() %>%
  addTiles() %>%
  addMarkers(lng=-95.38142, lat=29.80174, popup="Harris County.") %>%
  addMarkers(lng=-96.79868, lat=32.80453, popup="Dallas County.") 
```

***

An interactive map provides us with the flexibility to move the map around and to zoom in or out for greater details.