<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/mapping-traffic-fatalities/"></div></div><div><a data-url="https://www.r-bloggers.com/mapping-traffic-fatalities/" data-counturl="https://www.r-bloggers.com/mapping-traffic-fatalities/" data-count="horizontal" data-text="Mapping Traffic Fatalities" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://lucaspuente.github.io/notes/2016/09/01/Mapping_Traffic_Fatalities"> lucaspuente.github.io/</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>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), <a target="_blank" href="https://www.whitehouse.gov/blog/2016/08/29/2015-traffic-fatalities-data-has-just-been-released-call-action-download-and-analyze">announced</a> 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.</p>
<h3>Step 1: Download and Clean the Data</h3>
<p>The NHTSA made downloading this data set very easy. Simply visit <a target="_blank">ftp://ftp.nhtsa.dot.gov/fars/2015/National/</a> and download the <code>FARS2015NationalDBF.zip</code> file, unzip it, and load into R.</p>
<div>
<pre><code><span>library</span><span>(</span><span>foreign</span><span>)</span><span>
</span><span>accidents</span><span> </span><span>&lt;-</span><span> </span><span>read.dbf</span><span>(</span><span>&quot;FARS2015NationalDBF/accident.dbf&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>Since the goal here is to map the traffic fatalities, I also recommend subsetting the data to only include rows that have valid coordinates:</p>
<div>
<pre><code><span>accidents</span><span> </span><span>&lt;-</span><span> </span><span>subset</span><span>(</span><span>accidents</span><span>,</span><span> </span><span>LONGITUD</span><span>!=</span><span>999.99990</span><span> </span><span>&amp;</span><span> </span><span>LONGITUD</span><span>!=</span><span>888.88880</span><span> </span><span>&amp;</span><span> </span><span>LONGITUD</span><span>!=</span><span>777.77770</span><span>)</span><span>
</span></code></pre>
</div>
<p>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:</p>
<div>
<pre><code><span>cont_us_accidents</span><span>&lt;-</span><span>subset</span><span>(</span><span>accidents</span><span>,</span><span> </span><span>STATE</span><span>!=</span><span>2</span><span> </span><span>&amp;</span><span> </span><span>STATE</span><span>!=</span><span>15</span><span>)</span><span>
</span></code></pre>
</div>
<p>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 <code>map_data</code> function that’s part of the <code>ggplot2</code> package makes this step very easy:</p>
<div>
<pre><code><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>county_map_data</span><span>&lt;-</span><span>map_data</span><span>(</span><span>&quot;county&quot;</span><span>)</span><span>
</span><span>state_map</span><span> </span><span>&lt;-</span><span> </span><span>map_data</span><span>(</span><span>&quot;state&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<h3>Step 2: Plot the Data</h3>
<p>Plotting the data using <code>ggplot</code> is also not particularly complicated. The most important thing is to use layers. We’ll first add a polygon layer to a blank <code>ggplot</code> 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 (<code>alpha=0.05</code>) to help prevent points from obscuring one another.</p>
<div>
<pre><code><span>map</span><span>&lt;-</span><span>ggplot</span><span>()</span><span> </span><span>+</span><span> </span><span>#Add county borders:
</span><span> </span><span>geom_polygon</span><span>(</span><span>data</span><span>=</span><span>county_map_data</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span>=</span><span>long</span><span>,</span><span>y</span><span>=</span><span>lat</span><span>,</span><span>group</span><span>=</span><span>group</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>alpha</span><span>(</span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>1</span><span>/</span><span>4</span><span>),</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>0.2</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>NA</span><span>)</span><span> </span><span>+</span><span>
</span><span>#Add state borders:
</span><span> </span><span>geom_polygon</span><span>(</span><span>data</span><span> </span><span>=</span><span> </span><span>state_map</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span>=</span><span>long</span><span>,</span><span>y</span><span>=</span><span>lat</span><span>,</span><span>group</span><span>=</span><span>group</span><span>),</span><span> </span><span>colour</span><span> </span><span>=</span><span> </span><span>&quot;grey&quot;</span><span>,</span><span> </span><span>fill</span><span> </span><span>=</span><span> </span><span>NA</span><span>)</span><span> </span><span>+</span><span>
</span><span>#Add points (one per fatality):
</span><span> </span><span>geom_point</span><span>(</span><span>data</span><span>=</span><span>cont_us_accidents</span><span>,</span><span> </span><span>aes</span><span>(</span><span>x</span><span>=</span><span>LONGITUD</span><span>,</span><span> </span><span>y</span><span>=</span><span>LATITUDE</span><span>),</span><span> </span><span>alpha</span><span>=</span><span>0.05</span><span>,</span><span> </span><span>size</span><span>=</span><span>0.5</span><span>,</span><span> </span><span>col</span><span>=</span><span>&quot;red&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>#Adjust the map projection
</span><span> </span><span>coord_map</span><span>(</span><span>&quot;albers&quot;</span><span>,</span><span>lat0</span><span>=</span><span>39</span><span>,</span><span> </span><span>lat1</span><span>=</span><span>45</span><span>)</span><span> </span><span>+</span><span>
</span><span>#Add a title:
</span><span> </span><span>ggtitle</span><span>(</span><span>&quot;Traffic Fatalities in 2015&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>#Adjust the theme:
</span><span> </span><span>theme_classic</span><span>()</span><span> </span><span>+</span><span>
</span><span>theme</span><span>(</span><span>panel.border</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.text</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>line</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>axis.title</span><span> </span><span>=</span><span> </span><span>element_blank</span><span>(),</span><span>
</span><span>plot.title</span><span> </span><span>=</span><span> </span><span>element_text</span><span>(</span><span>size</span><span>=</span><span>40</span><span>,</span><span> </span><span>face</span><span>=</span><span>&quot;bold&quot;</span><span>,</span><span> </span><span>family</span><span>=</span><span>&quot;Avenir Next&quot;</span><span>))</span><span>
</span></code></pre>
</div>
<h3>Step 3: View the Finished Product</h3>
<p>With this relatively simple code, we produce a map that clearly displays the location of 2015’s traffic fatalities: <img src="https://i2.wp.com/raw.githubusercontent.com/LucasPuente/lucaspuente.github.io/master/img/Traffic%20Fatalities%20in%202015.png?resize=456%2C415&ssl=1" data-recalc-dims="1"></p>
<p>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 <a target="_blank" href="https://twitter.com/lucaspuente">@lucaspuente</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/mapping-traffic-fatalities/"></div></div><div><a data-url="https://www.r-bloggers.com/mapping-traffic-fatalities/" data-counturl="https://www.r-bloggers.com/mapping-traffic-fatalities/" data-count="vertical" data-text="Mapping Traffic Fatalities" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://lucaspuente.github.io/notes/2016/09/01/Mapping_Traffic_Fatalities"> lucaspuente.github.io/</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/GDHhvnKvxxI"></a></div>