Back in March 2016, I wrote about an extension to the <a target="_blank" href="https://www.r-project.org/">R</a> package <a target="_blank" href="https://cran.r-project.org/web/packages/ggplot2/index.html">ggplot2</a> that allowed subtitles to be added to charts. The process took a bit of fiddling and futzing, but now, with the release of ggplot2 version 2.2.0, it’s easy.<br><br>Let’s retrace the steps, and create a chart with a subtitle and a caption, the other nifty feature that has been added.<br><br>First, let’s read the packages we’ll be using, ggplot2 and the data carpentry package <a target="_blank" href="https://cran.r-project.org/web/packages/dplyr/">dplyr</a>:<br><br><pre><code># package load <br>library(ggplot2)<br>library(dplyr)</code></pre><div><h4>Read and summarize the data</h4>For this example, we’ll use the baseball data package <a target="_blank" href="https://cran.r-project.org/web/packages/Lahman/">Lahman</a> (bundling <a target="_blank" href="http://www.seanlahman.com/baseball-archive/statistics/">the Lahman database</a> for R users), and the data table ‘Teams’ in it.<br><br>Once it’s loaded, the data are filtered and summarized using dplyr.<br><ul><li>filter from 1901 [the establishment of the American League] to the most recent year,</li><li>filter out the Federal League</li><li>summarise the total number of runs scored, runs allowed, and games played</li><li>using `mutate`, calculate the league runs (<code>leagueRPG</code>) and runs allowed (<code>leagueRAPG</code>) per game</li></ul><pre><code>library(Lahman)<br>data(Teams)<br><br>MLB_RPG &lt;- Teams %>%<br>  filter(yearID > 1900, lgID != &quot;FL&quot;) %>%<br>  group_by(yearID) %>%<br>  summarise(R=sum(R), RA=sum(RA), G=sum(G)) %>%<br>  mutate(leagueRPG=R/G, leagueRAPG=RA/G)</code></pre></div><div><h4>A basic plot</h4>You may have heard that run scoring in Major League Baseball has been down in recent years…but what better way to see if that’s true than by plotting the data?<br><br>For the first version of the plot, we’ll make a basic X-Y plot, where the X axis has the years and the Y axis has the average number of runs scored. With ggplot2, it’s easy to add a trend line (the <code>geom_smooth</code> option).<br><br>The <code>scale_x_continuous</code> options set the limits and breaks of the axes.<br><br><pre><code>MLBRPGplot &lt;- ggplot(MLB_RPG, aes(x=yearID, y=leagueRPG)) +<br>  geom_point() +<br>  geom_smooth(span = 0.25) +<br>  scale_x_continuous(breaks = seq(1900, 2015, by = 20)) +<br>  scale_y_continuous(limits = c(3, 6), breaks = seq(3, 6, by = 1))<br><br>MLBRPGplot</code></pre><pre><code><br></code></pre><pre><code><br></code></pre><div><a href="https://1.bp.blogspot.com/-pStv6vIGL4Q/WCvfP1PJJVI/AAAAAAAAHnc/LJbnEMhLTpEVKRDb-8lF8OTvI0kXDopLwCLcB/s1600/2016-11-15_01.png"></a></div><br><br>So now we have a nice looking dot plot showing the average number of runs scored per game for the years 1901-2015. (The data for the 2016 season, recently concluded, has not yet been added to the Lahman database.)<br><br>With the basic plot object now created, we can make the changes in the format.  In the past, the way we would set the title, along with X and Y axis labels, would be something like this.<br><br><pre><code>MLBRPGplot +<br>  ggtitle(&quot;MLB run scoring, 1901-2014&quot;) +<br>  theme(plot.title = element_text(hjust=0, size=16)) +<br>  xlab(&quot;year&quot;) +<br>  ylab(&quot;team runs per game&quot;)</code></pre><pre><code><br></code></pre><pre><code><br></code></pre><div><a href="https://3.bp.blogspot.com/-R17wEp6VIWI/WCvfZFJGLnI/AAAAAAAAHng/PfeMN88LK6olA4X9qSSX1CqFoOnswaTlQCLcB/s1600/2016-11-15_02.png"></a></div></div><div><h3></h3><h3></h3><h4>Adding a subtitle and a caption: the function</h4><p></p>A popular feature of charts–particularly in magazines–is a subtitle that has a summary of what the chart shows and/or what the author wants to emphasize.<br><br>In this case, we could legitimately say something like any of the following:<br><ul><li>The peak of run scoring in the 2000 season has been followed by a steady drop</li><li>Teams scored 20% fewer runs in 2015 than in 2000</li><li>Team run scoring has fallen to just over 4 runs per game from the 2000 peak of 5 runs</li><li>Run scoring has been falling for 15 years, reversing a 30 year upward trend</li></ul>I like this last one, drawing attention not only to the recent decline but also the longer trend that started with the low-scoring environment of 1968.<br><br>How can we add a subtitle to our chart that does that, as well as a caption that acknowledges the source of the data? The new <code>labs</code> function, available in ggplot2 version 2.2.0, let's do that.<br><br>Note that <code>labs</code> contains the title, subtitle, caption, as well as the X and Y axis labels.<br><br><pre><code>MLBRPGplot +<br>  labs(title = &quot;MLB run scoring, 1901-2015&quot;,<br>       subtitle = &quot;Run scoring has been falling for 15 years, reversing a 30 year upward trend&quot;,<br>       caption = &quot;Source: the Lahman baseball database&quot;, <br>       x = &quot;year&quot;, y = &quot;team runs per game&quot;) </code></pre><pre><code><br></code></pre><pre><code><br></code></pre><div><a href="https://4.bp.blogspot.com/-yaBjzd8a9DA/WCvfi887l6I/AAAAAAAAHnk/o4p1s3ykK8Ylnh6b5mlk9ZvhheJ09TU7wCLcB/s1600/2016-11-15_03.png"></a></div><br><br>Easy.<br><br>Thanks to everyone involved with ggplot2 who made this possible.<br><br>The code for this post (as an R markdown file) can be found in my <a target="_blank" href="https://github.com/MonkmanMH/Bayesball">Bayesball github repo</a>.<br><br><br>-30-</div>