<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/adding-figure-labels-a-b-c-in-the-top-left-corner-of-the-plotting-region/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://logfc.wordpress.com/2017/03/15/adding-figure-labels-a-b-c-in-the-top-left-corner-of-the-plotting-region/"> r – log Fold Change</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I decided to submit a manuscript using only R with knitr, pandoc and make. Actually, it went quite well. Certainly, revisions of manuscript with complex figures did not require much of manual work once the R code for the figures has been created. The manuscript ended up as a Word file (for the sake of co-authors), looking no different than any other manuscript. However, you can look up precisely how all the figures have been generated and, with a single command, re-create the manuscript (with all figures and supplementary data) after you changed a parameter.</p>
<p>One of the small problems I faced was adding labels to pictures. You know — like A, B, C… in the top right corner of each panel of a composite figure. Here is the output I was striving at:</p>
<p><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://logfc.files.wordpress.com/2017/03/fig.png" data-orig-size="2096,2096" data-image-title="fig" data-comments-opened="1" class="wp-image-445" data-recalc-dims="1" data-attachment-id="445" data-permalink="https://logfc.wordpress.com/2017/03/15/adding-figure-labels-a-b-c-in-the-top-left-corner-of-the-plotting-region/fig/#main" alt="" data-medium-file="https://i0.wp.com/logfc.files.wordpress.com/2017/03/fig.png?resize=300%2C300&amp;ssl=1" data-large-file="https://logfc.files.wordpress.com/2017/03/fig.png?w=630" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i0.wp.com/logfc.files.wordpress.com/2017/03/fig.png?resize=300%2C300&ssl=1"></p>
<p>Doing it proved to be more tedious than I thought at first. By default, you can only plot things in the plotting region, everything else gets clipped — you cannot put arbitrary text anywhere outside the rectangle containing the actual plot:</p>
<pre>
plot(rnorm(100))
text(-20, 0, &quot;one two three four&quot;, cex=2)
</pre>
<p><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://logfc.files.wordpress.com/2017/03/fig1.png" data-orig-size="1363,1367" data-image-title="fig" data-comments-opened="1" class="wp-image-447" data-recalc-dims="1" data-attachment-id="447" data-permalink="https://logfc.wordpress.com/2017/03/15/adding-figure-labels-a-b-c-in-the-top-left-corner-of-the-plotting-region/fig-2/#main" alt="" data-medium-file="https://i2.wp.com/logfc.files.wordpress.com/2017/03/fig1.png?resize=300%2C300&amp;ssl=1" data-large-file="https://logfc.files.wordpress.com/2017/03/fig1.png?w=630" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i2.wp.com/logfc.files.wordpress.com/2017/03/fig1.png?resize=300%2C300&ssl=1"></p>
<p>This is <a target="_blank" rel="nofollow" href="http://www.melissaclarkson.com/resources/R_guides/documents/figure_layout_Ver1.pdf">because</a> the plotting are is the red rectangle on the figure below, and everything outside will not be shown by default:</p>
<p><img sizes="(max-width: 297px) 100vw, 297px" data-image-description="" data-orig-file="https://logfc.files.wordpress.com/2017/03/fig2.png" data-orig-size="1746,1763" data-image-title="fig" data-comments-opened="1" class="wp-image-448" data-recalc-dims="1" data-attachment-id="448" data-permalink="https://logfc.wordpress.com/2017/03/15/adding-figure-labels-a-b-c-in-the-top-left-corner-of-the-plotting-region/fig-3/#main" alt="" data-medium-file="https://i1.wp.com/logfc.files.wordpress.com/2017/03/fig2.png?resize=297%2C300&amp;ssl=1" data-large-file="https://logfc.files.wordpress.com/2017/03/fig2.png?w=630" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/logfc.files.wordpress.com/2017/03/fig2.png?resize=297%2C300&ssl=1"></p>
<p>One can use the function <code>mtext</code> to put text on the margins. However, there is no easy way to say “put the text in the top left corner of the figure”, and the results I was able to get were never perfect. Anyway, to push the label really to the very left of the figure region using <code>mtext</code>, you first need to have the user coordinate of that region (to be able to use option ‘at’). However, if you know these coordinates, it is much easier to achieve the desired effect using <code>text</code>.</p>
<p>However, we need to figure out a few things. First, to avoid clipping of the region, one needs to change the parameter <code>xpd</code>:</p>
<pre>
par(xpd=NA)
</pre>
<p>Then, we need to know where to draw the label. We can get the coordinates of the device (in inches), and then we can translate these to user coordinates with appropriate functions:</p>
<pre>
plot(rnorm(100))
di &lt;- dev.size(&quot;in&quot;)
x &lt;- grconvertX(c(0, di[1]), from=&quot;in&quot;, to=&quot;user&quot;)
y &lt;- grconvertY(c(0, di[2]), from=&quot;in&quot;, to=&quot;user&quot;)
</pre>
<p><code>x[1]</code> and <code>y[2]</code> are the coordinates of the top left corner of the device… but not of the figure. Since we might have used, for example, <code>par(mar=...)</code> or <code>layout</code> to put multiple plots on the device, and we would like to always label the current plot only (i.e. put the label in the corner of the current figure, not of the whole device), we have to take this into account as well:</p>
<pre>
fig &lt;- par(&quot;fig&quot;)
x &lt;- x[1] + (x[2] - x[1]) * fig[1:2]
y &lt;- y[1] + (y[2] - y[1]) * fig[3:4]
</pre>
<p>However, before plotting, we have to adjust this position by half of the text string width and height, respectively:</p>
<pre>
txt &lt;- &quot;A&quot;
x &lt;- x[1] + strwidth(txt, cex=3) / 2
y &lt;- y[2] - strheight(txt, cex=3) / 2
text(x, y, txt, cex=3)
</pre>
<p>Looks good! That is exactly what I wanted:</p>
<p><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://logfc.files.wordpress.com/2017/03/fig4.png" data-orig-size="2096,2096" data-image-title="fig" data-comments-opened="1" class="wp-image-450" data-recalc-dims="1" data-attachment-id="450" data-permalink="https://logfc.wordpress.com/2017/03/15/adding-figure-labels-a-b-c-in-the-top-left-corner-of-the-plotting-region/fig-5/#main" alt="" data-medium-file="https://i1.wp.com/logfc.files.wordpress.com/2017/03/fig4.png?resize=300%2C300&amp;ssl=1" data-large-file="https://logfc.files.wordpress.com/2017/03/fig4.png?w=630" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/logfc.files.wordpress.com/2017/03/fig4.png?resize=300%2C300&ssl=1"></p>
<p>Below you will find an R function that draws a label in one of the three regions — figure (default), plot or device. You specify the position of the label using the labels also used by <code>legend</code>: “topleft”, “bottomright” etc.</p>
<pre>
fig_label &lt;- function(text, region=&quot;figure&quot;, pos=&quot;topleft&quot;, cex=NULL, ...) {

  region &lt;- match.arg(region, c(&quot;figure&quot;, &quot;plot&quot;, &quot;device&quot;))
  pos &lt;- match.arg(pos, c(&quot;topleft&quot;, &quot;top&quot;, &quot;topright&quot;, 
                          &quot;left&quot;, &quot;center&quot;, &quot;right&quot;, 
                          &quot;bottomleft&quot;, &quot;bottom&quot;, &quot;bottomright&quot;))

  if(region %in% c(&quot;figure&quot;, &quot;device&quot;)) {
    ds &lt;- dev.size(&quot;in&quot;)
    # xy coordinates of device corners in user coordinates
    x &lt;- grconvertX(c(0, ds[1]), from=&quot;in&quot;, to=&quot;user&quot;)
    y &lt;- grconvertY(c(0, ds[2]), from=&quot;in&quot;, to=&quot;user&quot;)

    # fragment of the device we use to plot
    if(region == &quot;figure&quot;) {
      # account for the fragment of the device that 
      # the figure is using
      fig &lt;- par(&quot;fig&quot;)
      dx &lt;- (x[2] - x[1])
      dy &lt;- (y[2] - y[1])
      x &lt;- x[1] + dx * fig[1:2]
      y &lt;- y[1] + dy * fig[3:4]
    } 
  }

  # much simpler if in plotting region
  if(region == &quot;plot&quot;) {
    u &lt;- par(&quot;usr&quot;)
    x &lt;- u[1:2]
    y &lt;- u[3:4]
  }

  sw &lt;- strwidth(text, cex=cex) * 60/100
  sh &lt;- strheight(text, cex=cex) * 60/100

  x1 &lt;- switch(pos,
    topleft     =x[1] + sw, 
    left        =x[1] + sw,
    bottomleft  =x[1] + sw,
    top         =(x[1] + x[2])/2,
    center      =(x[1] + x[2])/2,
    bottom      =(x[1] + x[2])/2,
    topright    =x[2] - sw,
    right       =x[2] - sw,
    bottomright =x[2] - sw)

  y1 &lt;- switch(pos,
    topleft     =y[2] - sh,
    top         =y[2] - sh,
    topright    =y[2] - sh,
    left        =(y[1] + y[2])/2,
    center      =(y[1] + y[2])/2,
    right       =(y[1] + y[2])/2,
    bottomleft  =y[1] + sh,
    bottom      =y[1] + sh,
    bottomright =y[1] + sh)

  old.par &lt;- par(xpd=NA)
  on.exit(par(old.par))

  text(x1, y1, text, cex=cex, ...)
  return(invisible(c(x,y)))
}
</pre>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/logfc.wordpress.com/444/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/logfc.wordpress.com/444/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></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="https://logfc.wordpress.com/2017/03/15/adding-figure-labels-a-b-c-in-the-top-left-corner-of-the-plotting-region/"> r – log Fold Change</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/tzpz0msWYPs"></a></div>