<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/subtitles-and-captions-with-ggplot2-v-2-2-0/"></div></div><div><a data-url="https://www.r-bloggers.com/subtitles-and-captions-with-ggplot2-v-2-2-0/" data-counturl="https://www.r-bloggers.com/subtitles-and-captions-with-ggplot2-v-2-2-0/" data-count="horizontal" data-text="Subtitles and captions with ggplot2 v.2.2.0" 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://bayesball.blogspot.com/2016/11/sub-titles-and-captions-with-ggplot2.html"> Bayes Ball</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Back in March 2016, I wrote about an extension to the <a target="_blank" rel="nofollow" href="https://www.r-project.org/">R</a> package <a target="_blank" rel="nofollow" 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.</p>
<p>Let’s retrace the steps, and create a chart with a subtitle <em>and</em> a caption, the other nifty feature that has been added.</p>
<p>First, let’s read the packages we’ll be using, ggplot2 and the data carpentry package <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/dplyr/">dplyr</a>:</p>
<pre><code># package load <br>library(ggplot2)<br>library(dplyr)</code></pre>
<div>
<h4>Read and summarize the data</h4>
<p>For this example, we’ll use the baseball data package <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/Lahman/">Lahman</a> (bundling <a target="_blank" rel="nofollow" href="http://www.seanlahman.com/baseball-archive/statistics/">the Lahman database</a> for R users), and the data table ‘Teams’ in it.</p>
<p>Once it’s loaded, the data are filtered and summarized using dplyr.
<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>
<p>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?</p>
<p>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).</p>
<p>The <code>scale_x_continuous</code> options set the limits and breaks of the axes.</p>
<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 target="_blank" rel="nofollow" href="https://i2.wp.com/1.bp.blogspot.com/-pStv6vIGL4Q/WCvfP1PJJVI/AAAAAAAAHnc/LJbnEMhLTpEVKRDb-8lF8OTvI0kXDopLwCLcB/s1600/2016-11-15_01.png?ssl=1"><img src="https://i0.wp.com/1.bp.blogspot.com/-pStv6vIGL4Q/WCvfP1PJJVI/AAAAAAAAHnc/LJbnEMhLTpEVKRDb-8lF8OTvI0kXDopLwCLcB/s320/2016-11-15_01.png?resize=320%2C272&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>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.)</p>
<p>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.</p>
<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 target="_blank" rel="nofollow" href="https://i0.wp.com/3.bp.blogspot.com/-R17wEp6VIWI/WCvfZFJGLnI/AAAAAAAAHng/PfeMN88LK6olA4X9qSSX1CqFoOnswaTlQCLcB/s1600/2016-11-15_02.png?ssl=1"><img src="https://i1.wp.com/3.bp.blogspot.com/-R17wEp6VIWI/WCvfZFJGLnI/AAAAAAAAHng/PfeMN88LK6olA4X9qSSX1CqFoOnswaTlQCLcB/s320/2016-11-15_02.png?resize=320%2C272&ssl=1" border="0" data-recalc-dims="1"></a></div>
</div>
<div>
<h3></h3>
<h3></h3>
<h4>Adding a subtitle and a caption: the function</h4>

<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.</p>
<p>In this case, we could legitimately say something like any of the following:
<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>
<p>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.</p>
<p>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.</p>
<p>Note that <code>labs</code> contains the title, subtitle, caption, as well as the X and Y axis labels.</p>
<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 target="_blank" rel="nofollow" href="https://i2.wp.com/4.bp.blogspot.com/-yaBjzd8a9DA/WCvfi887l6I/AAAAAAAAHnk/o4p1s3ykK8Ylnh6b5mlk9ZvhheJ09TU7wCLcB/s1600/2016-11-15_03.png?ssl=1"><img src="https://i0.wp.com/4.bp.blogspot.com/-yaBjzd8a9DA/WCvfi887l6I/AAAAAAAAHnk/o4p1s3ykK8Ylnh6b5mlk9ZvhheJ09TU7wCLcB/s320/2016-11-15_03.png?resize=320%2C272&ssl=1" border="0" data-recalc-dims="1"></a></div>
<p>Easy.</p>
<p>Thanks to everyone involved with ggplot2 who made this possible.</p>
<p>The code for this post (as an R markdown file) can be found in my <a target="_blank" rel="nofollow" href="https://github.com/MonkmanMH/Bayesball">Bayesball github repo</a>.</p>
<p>-30-</div>
<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/subtitles-and-captions-with-ggplot2-v-2-2-0/"></div></div><div><a data-url="https://www.r-bloggers.com/subtitles-and-captions-with-ggplot2-v-2-2-0/" data-counturl="https://www.r-bloggers.com/subtitles-and-captions-with-ggplot2-v-2-2-0/" data-count="vertical" data-text="Subtitles and captions with ggplot2 v.2.2.0" 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://bayesball.blogspot.com/2016/11/sub-titles-and-captions-with-ggplot2.html"> Bayes Ball</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/lImcCVtG4ec"></a></div>