<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/how-to-annotate-a-plot-in-ggplot2/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://sharpsightlabs.com/blog/simple-annotate-plot-ggplot2/"> r-bloggers – SHARP SIGHT LABS</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 1516px) 100vw, 1516px" alt="" class="wp-image-3062" src="https://i1.wp.com/sharpsightlabs.com/wp-content/uploads/2017/02/histogram_gonna-annotate-right-here.png?resize=456%2C285" data-recalc-dims="1"><br>
</a></p>
<p>“Master the basics.”</p>
<p>That’s a common mantra here at Sharp Sight.</p>
<p>Loyal readers know what I mean by “master the basics.” To master data science, you need to <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/stop-trying-jump-sexy-stuff/">master the foundational tools</a>.</p>
<p>That means knowing how to create essential plots like:</p>
<ul>
<li>Bar charts</li>
<li>Line charts</li>
<li>Histograms</li>
</ul>
<p>And <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/dplyr-intro-data-manipulation-with-r/">performing data manipulations</a> like:</p>
<ul>
<li>Creating new variables</li>
<li>Subsetting</li>
<li>Aggregating</li>
<li>Sorting</li>
<li>Joining datasets</li>
</ul>
<h2>After the foundations, the little details matter<br>
(like annotations)</h2>
<p>Having said that, the little details matter. </p>
<p>After you master the basics, you need to learn the little details.</p>
<p>A great example of this is plot annotation.</p>
<p>Adding little details like plot annotations help you communicate more clearly and “tell a story” with your plots.</p>
<p>Moreover, annotations help your data visualizations “stand on their own.” </p>
<p>What I mean by this, is that they help your plots communicate and “tell a story,” without <em>you</em> being there to do the communicating. </p>
<p>Certainly, as a data scientist, if you create a report or analysis there will be many instances where you will <em>personally</em> present your work to an audience. In these instances, you will <em>be there</em> to explain your work and give a personal “voice over” for your visualizations.</p>
<p>But that’s not always the case. For example, you’ll often send your analyses to partners as reference documents. Other times, you’ll publish them (maybe on a blog, or internal company website). In these instances, <em>you</em> won’t be there to explain your work. You need your work to “speak for you.” The charts need to communicate on their own. To make sure that your work is still effective when you are <strong>not</strong> there, you’ll often need to annotate your work.</p>
<p>Therefore, annotations are one of the “little details” that you need to learn after you learn the foundations.</p>
<h2>A simple annotation in ggplot2</h2>
<p>Here, I’ll show you a very simple example of a plot annotation in .</p>
<p>Before I show it to you though, I want to introduce you to a learning principle that you can use when you’re learning and practicing data science techniques.</p>
<h3>To learn , start with a very simple example</h3>
<p>When you’re learning a new skill, it’s very effective to learn and practice with very simple examples. The simpler the better. </p>
<p>As a side note, this is one of the reasons that I strongly discourage the “jump in and build something” method of learning. When people “jump in” and work on a project, they typically select something that’s far too complicated, so they spend all of their time struggling to do simple things that they should have learned <em>before</em> working on a project.</p>
<p>So before you jump into a project, learn individual techniques. And when you’re learning a new technique, use very, very basic examples.</p>
<h2>Code: how to create an annotation in ggplot2</h2>
<p>With that in mind, I’ll show you a very, very simple plot annotation in .</p>
<p>Here we have a histogram with a dashed line at the mean. The mean is 5.</p>
<pre class="wp-code-highlight">
library(ggplot2)

set.seed(10)
df.rnorm &lt;- data.frame(rnorm = rnorm(10000, mean = 5))

ggplot(data = df.rnorm, aes(x = rnorm)) +
  geom_histogram(bins = 50) +
  geom_vline(xintercept = 5, color = &quot;red&quot;, linetype = &quot;dashed&quot;) 
</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 1876px) 100vw, 1876px" alt="" class="wp-image-3060" src="https://i0.wp.com/sharpsightlabs.com/wp-content/uploads/2017/02/Screen-Shot-2017-02-25-at-8.32.50-PM.png?resize=456%2C251" data-recalc-dims="1"><br>
</a></p>
<p>We want to add an annotation that explicitly calls out the value of the mean. </p>
<h2>Add an annotation with the  function</h2>
<pre class="wp-code-highlight">
ggplot(data = df.rnorm, aes(x = rnorm)) +
  geom_histogram(bins = 50) +
  geom_vline(xintercept = 5, color = &quot;red&quot;, linetype = &quot;dashed&quot;) +
  annotate(&quot;text&quot;, label = &quot;Mean = 5&quot;, x = 4.5, y = 170, color = &quot;white&quot;)
</pre>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/"><br>
<img sizes="(max-width: 1388px) 100vw, 1388px" alt="" class="wp-image-3061" src="https://i2.wp.com/sharpsightlabs.com/wp-content/uploads/2017/02/histogram_annotated_mean5.png?resize=456%2C266" data-recalc-dims="1"><br>
</a></p>
<p>Here, we’ve added the annotation that says “Mean = 5.” </p>
<p>This is very straightforward. </p>
<p>To accomplish this, we’ve used the  function.</p>
<p>The first argument of the function is . This specifies that we want to use a <em>text</em> annotation. As it turns out, there are several different annotation types, including <em>rectangles</em> and <em>line segments</em>, so you need to specify exactly what type of annotation you want to add. Because we’re adding a text annotation,  is the appropriate option.</p>
<p>The next piece of syntax within  is the  parameter.  just specifies the exact text that we want to add to the plot. Here, we’re specifying that we want to add the text .</p>
<p>Next, we specify the exact location of the annotation by detailing the x and y coordinates with the  and  parameters respectively.</p>
<p>Finally, we specify the color. In this case, I’ve set the color to . </p>
<p>You could simplify this code <em>even further</em> by removing the color specification and letting it default to . When you practice making annotations, I would probably <em>recommend</em> that you remove the color specification for the sake of simplicity. (You are practicing R, right?)</p>
<p>Having said that, in this case, the white text looks best against the dark grey histogram, so I left that code in.</p>
<h2>To remember the  technique, you need to practice it</h2>
<p> is a fairly simple technique.</p>
<p>Nevertheless, my bet is that many people will forget it in the long run because they fail to <em>practice</em> it. They’ll “cut and paste” once or twice, and then quickly forget it.</p>
<p>I’ve said before that it’s not enough just to learn a new technique. You need to <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/data-learning-vs-remembering/">remember it in the long run</a>.</p>
<p>The problem is that even if you learn a new ggplot2 technique today, you’re very, very likely to forget within a few days.</p>
<p>Having said that, if you want to <em>remember</em> how to use  and other R tools – if you <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/master-r-obsolete/">want to <em>master</em> R</a> – you need to <u>practice</u>.</p>
<h1>Sign up to learn </h1>
<p>Discover how to rapidly master  and other R data science tools.</p>
<p><a href="http://sharpsightlabs.com/join-newsletter-ggplot2/">Sign up for our email list HERE</a>.</p>
<p>If you sign up, you’ll get <i>free</i> tutorials about  and other R tools, delivered to your inbox.</p>
<p>The post <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/blog/simple-annotate-plot-ggplot2/">How to annotate a plot in ggplot2</a> appeared first on <a target="_blank" rel="nofollow" href="http://sharpsightlabs.com/">SHARP SIGHT LABS</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://sharpsightlabs.com/blog/simple-annotate-plot-ggplot2/"> r-bloggers – SHARP SIGHT LABS</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/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/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/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/ga2gw3ZgXDI"></a></div>