Mapping Traffic Fatalities

[This article was first published on lucaspuente.github.io/, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

On Monday, August 29, DJ Patil, the Chief Data Scientist in the White House Office of Science and Technology Policy, and Mark Rosekind, the Administrator of the National Highway Traffic Safety Administration (NHTSA), announced the release of a data set documenting all traffic fatalities occurring in the United States in 2015. As part of their release, they issued a “call to action” for data scientists and analysts to “jump in and analyze it.” This post does exactly that by plotting these fatalities and providing the code for others to reproduce and extend the analysis.

Step 1: Download and Clean the Data

The NHTSA made downloading this data set very easy. Simply visit ftp://ftp.nhtsa.dot.gov/fars/2015/National/ and download the FARS2015NationalDBF.zip file, unzip it, and load into R.

<span class="n">library</span><span class="p">(</span><span class="n">foreign</span><span class="p">)</span><span class="w">
</span><span class="n">accidents</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">read.dbf</span><span class="p">(</span><span class="s2">"FARS2015NationalDBF/accident.dbf"</span><span class="p">)</span><span class="w">
</span>

Since the goal here is to map the traffic fatalities, I also recommend subsetting the data to only include rows that have valid coordinates:

<span class="n">accidents</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">subset</span><span class="p">(</span><span class="n">accidents</span><span class="p">,</span><span class="w"> </span><span class="n">LONGITUD</span><span class="o">!=</span><span class="m">999.99990</span><span class="w"> </span><span class="o">&</span><span class="w">  </span><span class="n">LONGITUD</span><span class="o">!=</span><span class="m">888.88880</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">LONGITUD</span><span class="o">!=</span><span class="m">777.77770</span><span class="p">)</span><span class="w">
</span>

Also, the map we’ll be producing will only include the lower 48 states, so we want to further subset the data to exclude Alaska and Hawaii:

<span class="n">cont_us_accidents</span><span class="o"><-</span><span class="n">subset</span><span class="p">(</span><span class="n">accidents</span><span class="p">,</span><span class="w"> </span><span class="n">STATE</span><span class="o">!=</span><span class="m">2</span><span class="w"> </span><span class="o">&</span><span class="w"> </span><span class="n">STATE</span><span class="o">!=</span><span class="m">15</span><span class="p">)</span><span class="w">
</span>

We also need to load in data on state and county borders to make our map more interpretable – without this, there would be no borders on display. Fortunately, the map_data function that’s part of the ggplot2 package makes this step very easy:

<span class="n">library</span><span class="p">(</span><span class="n">ggplot2</span><span class="p">)</span><span class="w">
</span><span class="n">county_map_data</span><span class="o"><-</span><span class="n">map_data</span><span class="p">(</span><span class="s2">"county"</span><span class="p">)</span><span class="w">
</span><span class="n">state_map</span><span class="w"> </span><span class="o"><-</span><span class="w"> </span><span class="n">map_data</span><span class="p">(</span><span class="s2">"state"</span><span class="p">)</span><span class="w">
</span>

Step 2: Plot the Data

Plotting the data using ggplot is also not particularly complicated. The most important thing is to use layers. We’ll first add a polygon layer to a blank ggplot object to map the county borders in light grey and then subsequently add polygons to map the state borders. Then, we’ll add points to show exactly where in the (lower 48) United States traffic fatalities occurred in 2015, plotting these in red, but with a high level of transparency (alpha=0.05) to help prevent points from obscuring one another.

<span class="n">map</span><span class="o"><-</span><span class="n">ggplot</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w"> 
  </span><span class="c1">#Add county borders:
</span><span class="w">  </span><span class="n">geom_polygon</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">county_map_data</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">long</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="n">lat</span><span class="p">,</span><span class="n">group</span><span class="o">=</span><span class="n">group</span><span class="p">),</span><span class="w"> </span><span class="n">colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">alpha</span><span class="p">(</span><span class="s2">"grey"</span><span class="p">,</span><span class="w"> </span><span class="m">1</span><span class="o">/</span><span class="m">4</span><span class="p">),</span><span class="w"> </span><span class="n">size</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="m">0.2</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NA</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="c1">#Add state borders:
</span><span class="w">  </span><span class="n">geom_polygon</span><span class="p">(</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">state_map</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">long</span><span class="p">,</span><span class="n">y</span><span class="o">=</span><span class="n">lat</span><span class="p">,</span><span class="n">group</span><span class="o">=</span><span class="n">group</span><span class="p">),</span><span class="w"> </span><span class="n">colour</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s2">"grey"</span><span class="p">,</span><span class="w"> </span><span class="n">fill</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kc">NA</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="c1">#Add points (one per fatality):
</span><span class="w">  </span><span class="n">geom_point</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">cont_us_accidents</span><span class="p">,</span><span class="w"> </span><span class="n">aes</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">LONGITUD</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="o">=</span><span class="n">LATITUDE</span><span class="p">),</span><span class="w"> </span><span class="n">alpha</span><span class="o">=</span><span class="m">0.05</span><span class="p">,</span><span class="w"> </span><span class="n">size</span><span class="o">=</span><span class="m">0.5</span><span class="p">,</span><span class="w"> </span><span class="n">col</span><span class="o">=</span><span class="s2">"red"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="c1">#Adjust the map projection
</span><span class="w">  </span><span class="n">coord_map</span><span class="p">(</span><span class="s2">"albers"</span><span class="p">,</span><span class="n">lat0</span><span class="o">=</span><span class="m">39</span><span class="p">,</span><span class="w"> </span><span class="n">lat1</span><span class="o">=</span><span class="m">45</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="c1">#Add a title:
</span><span class="w">  </span><span class="n">ggtitle</span><span class="p">(</span><span class="s2">"Traffic Fatalities in 2015"</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="c1">#Adjust the theme:
</span><span class="w">  </span><span class="n">theme_classic</span><span class="p">()</span><span class="w"> </span><span class="o">+</span><span class="w">
  </span><span class="n">theme</span><span class="p">(</span><span class="n">panel.border</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">axis.text</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">line</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">axis.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_blank</span><span class="p">(),</span><span class="w">
        </span><span class="n">plot.title</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">element_text</span><span class="p">(</span><span class="n">size</span><span class="o">=</span><span class="m">40</span><span class="p">,</span><span class="w"> </span><span class="n">face</span><span class="o">=</span><span class="s2">"bold"</span><span class="p">,</span><span class="w"> </span><span class="n">family</span><span class="o">=</span><span class="s2">"Avenir Next"</span><span class="p">))</span><span class="w">
</span>

Step 3: View the Finished Product

With this relatively simple code, we produce a map that clearly displays the location of 2015’s traffic fatalities:

Hopefully with this post you’ll be well on the way to making maps of your own and can start exploring this data set and others like it. If you have any questions, please reach out on twitter. I’m available @lucaspuente.

To leave a comment for the author, please follow the link and comment on their blog: lucaspuente.github.io/.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)