<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/data-viz-challenge-recreating-fivethirtyeights-deadest-names-graphic-with-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://feedproxy.google.com/~r/My-misadventures-In-R-Programming/~3/IaTAZAdAqDY/"> my (mis)adventures in R programming</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’ve recently begun reading through the book <em><a rel="nofollow" href="https://mdsr-book.github.io/" target="_blank">Modern Data Science with R, by Benjamin S. Baumer, Daniel T. Kaplan, and Nicholas J. Horton</a></em>. It’s quite clear and informative. One of the things I especially appreciate about it is that I’m not finding the math to be too cumbersome. That is, even for someone like me, whose primary background isn’t in math or statistics, I’m able to follow along with the book quite easily.</p>
<p>As I’m reading through the book, I’m doing the exercises at the back of the chapters, and I recently worked through chapter 3, which covers <code>ggplot2</code> basics. One of the exercises at the end of this chapter asks us to recreate <a rel="nofollow" href="http://tinyurl.com/zcbc19o" target="_blank">this graphic</a> from <a rel="nofollow" href="https://fivethirtyeight.com/" target="_blank">FiveThirtyEight</a>. The goal of the exercise is to use <code>ggplot2</code> to make production-quality graphics.</p>
<p>The project makes use of the <code>babynames</code> package, which uses public data on baby names from the Social Security Administration. We then use the <code>make_babynames_dist()</code> function from the <code>mdsr</code> package that the authors developed to add variables relevant to the goals of the exercise. Basically, it takes the data from the <code>lifetables</code> table in the <code>babynames</code> package and adds variables and filters and returns just the data relevant to 2014.</p>
<p>Truth told, I was stumped by this exercise when I first read it. So I reached out to Nicholas Horton at Amherst and he helped me with the basic scripting and I was able to tweak it to recreate what I was looking for.</p>
<p>So first we load the necessary libraries and inspect the dataset.</p>

<pre>library(mdsr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggthemes)
library(babynames)

babynames_dist &lt;- make_babynames_dist()
head(babynames_dist)</pre>

<pre># A tibble: 6 x 9
   year sex   name       n   prop alive_prob count_thousands age_today est_alive_today
  </pre>