<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/a-fast-method-to-add-annotations-to-a-plot/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.seascapemodels.org/rstats/2017/03/29/locator-to-clipboard.html"> Bluecology blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<h2>A fast method to add annotations to a plot</h2>
<p>Making professional looking plots in R can be fiddly. One task that I often spend ages doing is manually finding coordinates to add labels.<br>
Wouldn’t it be nice if you could just send the coordinates directly to your text editor?</p>
<p>I did some searching and found on <a target="_blank" rel="nofollow" href="http://stackoverflow.com/questions/14547069/how-to-write-from-r-to-the-clipboard-on-a-mac">stackoverflow</a> that you can send R objects to the clipboard. So here is my solution using that trick.</p>
<div>
<img alt="" src="https://i0.wp.com/www.seascapemodels.org/Images/locator-plot.png?w=456" data-recalc-dims="1">
<p> Adding text to the right position on a plot can be a real hassle. Here I show how to use a simple function to click on a figurea and put coordinates onto your clipboard. </p>
</div>
<p>You can get R to send data directly to the clipboard using the <code>pipe</code> command. Below is a little function I wrote that takes coordinates from <code>locator()</code> and sends them to your clipboard. Then you can just hit cmd-v to paste them into your text editor (nb I understand this may need some slight modifications to work on linux or windows, I use OSX):</p>
<div>
<pre><code>loccopy &lt;- function(n, digits = 2){
    data &lt;- locator(n)
    data &lt;- round(cbind(data$x, data$y), digits)
    clip &lt;- pipe(&quot;pbcopy&quot;, &quot;w&quot;)
    write.table(data, file = clip, col.names = F, row.names = F)
    close(clip)
}
</code></pre>
</div>
<p>Let’s test it out:</p>
<div>
<pre><code>set.seed(42)
plot(runif(100))
loccopy(1)
</code></pre>
</div>
<p>Now hit cmd-v (or equivalent on your OS).</p>
<div>
<pre><code>69.23 0.84
</code></pre>
</div>
<p>Let’s add a label using our fast method for coordinates:</p>
<div>
<pre><code>text(69.23, 0.84, &quot;Unusual data point&quot;, pos =4, offset = 0)
</code></pre>
</div>
<p>The <code>pos=4</code> and <code>offset=0</code> ensures that the text goes directly to the right of our coordinates.</p>
<p>That’s it. Hope it helps speed up your workflow.</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://www.seascapemodels.org/rstats/2017/03/29/locator-to-clipboard.html"> Bluecology blog</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/DnlzNBML_mM"></a></div>