<p><a href="http://michaeltoth.me/installing-and-running-shiny-server-from-source-on-32-bit-ubuntu.html">A while back</a> I spent some time figuring out how to serve interactive shiny apps through my website, but I haven't had a chance to build anything until recently. I set out to create a few simple shiny apps in R that I could use as a sort of test run, and I'm writing those up here.</p>
<p>In this post I'm going to be analyzing some open data provided by the Social Security Administration on the popularity of baby names over the years--specifically, since 1880. Data comes from the <a href="https://www.ssa.gov/oact/babynames/limits.html">Social Security Administration</a> </p>
<p>In the application below, you can view the 10 most popular baby names for any year since 1880, either for males or females. You can also click the play icon directly below the year slider to view an animated history of the most common names.
<br><br></p>
<p>In the next application, you can enter any name, and the graph will display how the popularity of that name has changed over time. Be sure to also select whether the name is for males or females, or you'll likely see some unexpected results!
<br><br></p>
<p>After building the shiny applications above, I got interested in whether I could identify any meaningful trends over time in the data. I wanted to see whether the concentration (the proportion of babies born with a given name) of the most popular names was relatively static over time, or whether this fluctuated. I was also interested in finding trends in the number of babies born with each of the most popular names. To investigate these, I used a subset of the original data, grabbing the 10 most common male and female names for each year since 1880. I went through several iterations of how best to display the data, and ultimately arrived at the graph below, which I quite like. </p>
<p>I was excited that this project gave me an opportunity to make use of David Robinson's <a href="https://github.com/dgrtwo/gganimate">gganimate package</a>, which I must regrettably admit I hadn't had a chance to experiment with previously. For those unfamiliar, this package makes it incredibly easy to create animated ggplot graphs, and it's awesome!</p>
<p>I wanted to create some kind of trailing visualization to make it clear how patterns and trends were changing over time. The implementation here was adapted from Thomas Pedersen's <a href="https://gist.github.com/thomasp85/c8e22be4628e4420d4f66bcc6c88ac87">example</a> which he used to produce <a href="https://twitter.com/thomasp85/status/694905779539812352">this image</a>. </p>
<p>In the graph below we can use the trailing effect to easily identify trends that occur over a series of years. The grey background data also helps us to visualize how any given year compares with the overall history. I see 5 key periods present themselves in the data:</p>
<ul><li><strong>Early Years (1880 - 1910)</strong>: This period is characterized by a low number of babies born (due to the much lower population at this time) as well as a high concentration of the most popular names, in some cases reaching almost 10%. This means that the most common names were being used by a very high percentage of the population during this period. Toward the end of this time period, we begin to see declines in the concentration statistic.</li>
<li><strong>World War I Years (1910 - 1920)</strong>: In this period we see an explosion in the number of babies born with the most popular names. We don't see much change in the concentration of names over this period.</li>
<li><strong>Intra-War Period (1921 - 1940)</strong>: In this period both the number and the concentration of births is remarkably consistent, with almost no changes on a year-over-year basis.</li>
<li><strong>WWII &amp; Baby Boom (1941 - 1957)</strong>: In this period we again see a huge increase in the number of babies born, corresponding to the well-known baby boom that occurred in the post-WWII years. We actually see this increase beginning during the war, in 1941. We also see a slight decrease in the concentration of the most popular baby names during this period.</li>
<li><strong>&quot;Modern&quot; Era (1958 - 2015)</strong>: This period is characterized by a steady decrease in both the concentration of the most popular names and in the number of babies born with those names. Though overall birth rates did begin to decline in recent years, this change is not due primarily to a decline in birth rates, but rather to an increased equity in the popularity of names, which means that the most popular names make up a much smaller percentage of overall births.
</li>
</ul><p>The code for this image is available below:</p>
<div><pre><span></span><span>library</span><span>(</span>dplyr<span>)</span>
<span>library</span><span>(</span>ggplot2<span>)</span>
<span>library</span><span>(</span>gganimate<span>)</span> <span>#devtools::install_github(&quot;dgrtwo/gganimate&quot;)</span>
<span>library</span><span>(</span>readr<span>)</span>
<span>library</span><span>(</span>scales<span>)</span>

<span># Load pre-processed data. For additional details check Github below</span>
top_10_each_year <span>&lt;-</span> read_csv<span>(</span><span>'input/top_10_each_year.csv'</span><span>)</span>

<span># Create fading animation effect by replicating the data frame and adding an exponentially decaying fade parameter to previous years</span>
anim <span>&lt;-</span> <span>lapply</span><span>(</span><span>1</span><span>:</span><span>10</span><span>,</span> <span>function</span><span>(</span>i<span>)</span> <span>{</span>top_10_each_year<span>$</span>year <span>&lt;-</span> top_10_each_year<span>$</span>year <span>+</span> i<span>;</span> top_10_each_year<span>$</span>fade <span>&lt;-</span> <span>1</span> <span>/</span> <span>(</span>i <span>+</span> <span>2</span><span>);</span> top_10_each_year<span>})</span>
top_10_each_year<span>$</span>fade <span>&lt;-</span> <span>1</span>
top_10_with_fade <span>&lt;-</span> <span>rbind</span><span>(</span>top_10_each_year<span>,</span> <span>do.call</span><span>(</span><span>rbind</span><span>,</span> anim<span>))</span>
top_10_with_fade <span>&lt;-</span> filter<span>(</span>top_10_with_fade<span>,</span> year <span>&lt;=</span> <span>2015</span><span>)</span>

p <span>&lt;-</span> ggplot<span>(</span>top_10_with_fade<span>,</span> aes<span>(</span>x <span>=</span> proportion<span>,</span> y <span>=</span> count<span>))</span> <span>+</span>
    geom_point<span>(</span>color <span>=</span> <span>'#e6e6e6'</span><span>,</span> size <span>=</span> <span>4</span><span>)</span> <span>+</span>
    geom_point<span>(</span>aes<span>(</span>color <span>=</span> sex<span>,</span> frame <span>=</span> year<span>,</span> alpha <span>=</span> fade<span>),</span> size <span>=</span> <span>4</span><span>)</span> <span>+</span>
    ggtitle<span>(</span><span>'Top 10 Male &amp; Female Baby Names\nYear:'</span><span>)</span> <span>+</span>
    xlab<span>(</span><span>'\nProportion (by sex) Born with Name'</span><span>)</span> <span>+</span>
    ylab<span>(</span><span>'Number Born with Name'</span><span>)</span> <span>+</span>
    scale_color_manual<span>(</span>name <span>=</span> <span>''</span><span>,</span> values <span>=</span> <span>c</span><span>(</span><span>'#ff7f00'</span><span>,</span> <span>'#377eb8'</span><span>),</span> labels <span>=</span> <span>c</span><span>(</span><span>'Female'</span><span>,</span> <span>'Male'</span><span>))</span> <span>+</span>
    scale_x_continuous<span>(</span>labels <span>=</span> percent<span>)</span> <span>+</span> 
    scale_y_continuous<span>(</span>labels <span>=</span> comma<span>)</span> <span>+</span>
    scale_alpha<span>(</span>guide <span>=</span> <span>'none'</span><span>)</span> <span>+</span> <span># Remove alpha legend from plot output</span>
    theme_bw<span>()</span> <span>+</span>
    theme<span>(</span>panel.border <span>=</span> element_blank<span>(),</span>
          panel.grid <span>=</span> element_blank<span>(),</span>
          axis.ticks <span>=</span> element_blank<span>(),</span>
          legend.key <span>=</span> element_blank<span>(),</span>
          legend.position <span>=</span> <span>'bottom'</span><span>,</span>
          axis.text <span>=</span> element_text<span>(</span>size <span>=</span> <span>14</span><span>),</span>
          axis.title <span>=</span> element_text<span>(</span>size <span>=</span> <span>16</span><span>),</span>
          legend.text <span>=</span> element_text<span>(</span>size <span>=</span> <span>12</span><span>))</span>

gg_animate<span>(</span>p<span>,</span> filename <span>=</span> <span>'yearly-birth-names-with-trails.gif'</span><span>,</span> interval <span>=</span> <span>0.2</span><span>,</span> ani.width <span>=</span> <span>800</span><span>,</span> ani.height <span>=</span> <span>600</span><span>)</span>
</pre></div>
<p>For the full code behind the shiny applications and the animation produced above, check out my <a href="https://github.com/michaeltoth/shiny-projects/tree/master/census_names">Github</a></p>