237 lines
169 KiB
HTML
Raw Normal View History

2025-01-12 00:52:51 +08:00
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Baptiste Auguie" />
<meta name="date" content="2017-09-09" />
<title>(Unofficial) overview of gtable</title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<link href="data:text/css;charset=utf-8,body%20%7B%0Abackground%2Dcolor%3A%20%23fff%3B%0Amargin%3A%201em%20auto%3B%0Amax%2Dwidth%3A%20800px%3B%0Aoverflow%3A%20visible%3B%0Apadding%2Dleft%3A%202em%3B%0Apadding%2Dright%3A%202em%3B%0Afont%2Dfamily%3A%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0Afont%2Dsize%3A%2014px%3B%0Aline%2Dheight%3A%2020px%3B%0A%7D%0A%23header%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0A%23TOC%20%7B%0Aclear%3A%20both%3B%0Amargin%3A%200%200%2010px%200%3B%0Apadding%3A%204px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Aborder%2Dradius%3A%205px%3B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Afont%2Dsize%3A%2013px%3B%0Aline%2Dheight%3A%201%2E3%3B%0A%7D%0A%23TOC%20%2Etoctitle%20%7B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%2015px%3B%0Amargin%2Dleft%3A%205px%3B%0A%7D%0A%23TOC%20ul%20%7B%0Apadding%2Dleft%3A%2040px%3B%0Amargin%2Dleft%3A%20%2D1%2E5em%3B%0Amargin%2Dtop%3A%205px%3B%0Amargin%2Dbottom%3A%205px%3B%0A%7D%0A%23TOC%20ul%20ul%20%7B%0Amargin%2Dleft%3A%20%2D2em%3B%0A%7D%0A%23TOC%20li%20%7B%0Aline%2Dheight%3A%2016px%3B%0A%7D%0Atable%3Anot%28%5Bclass%5D%29%20%7B%0Amargin%3A%20auto%3B%0Amin%2Dwidth%3A%2040%25%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20%23DDDDDD%3B%0Aborder%2Dstyle%3A%20outset%3B%0Aborder%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%5Bsummary%3D%22R%20argblock%22%5D%20%7B%0Awidth%3A%20100%25%3B%0Aborder%3A%20none%3B%0A%7D%0Atable%3Anot%28%5Bclass%5D%29%20th%20%7B%0Aborder%2Dwidth%3A%202px%3B%0Apadding%3A%205px%3B%0Aborder%2Dstyle%3A%20inset%3B%0A%7D%0Atable%3Anot%28%5Bclass%5D%29%20td%20%7B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dstyle%3A%20inset%3B%0Aline%2Dheight%3A%2018px%3B%0Apadding%3A%205px%205px%3B%0A%7D%0Atable%3Anot%28%5Bclass%5D%29%2C%20table%3Anot%28%5Bclass%5D%29%20th%2C%20table%3Anot%28%5Bclass%5D%29%20td%20%7B%0Aborder%2Dleft%2Dstyle%3A%20none%3B%0Aborder%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%3Anot%28%5Bclass%5D%29%20tr%2Eodd%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Ap%20%7B%0Amargin%3A%200%2E5em%200%3B%0A%7D%0Ablockquote%20%7B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Apadding%3A%2013px%3B%0Apadding%2Dbottom%3A%201px%3B%0A%7D%0Ahr%20%7B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%3A%20none%3B%0Aborder%2Dtop%3A%201px%20solid%20%23777%3B%0Amargin%3A%2028px%200%3B%0A%7D%0Adl%20%7B%0Amargin%2Dleft%3A%200%3B%0A%7D%0Adl%20dd%20%7B%0Amargin%2Dbottom%3A%2013px%3B%0Amargin%2Dleft%3A%2013px%3B%0A%7D%0Adl%20dt%20%7B%0Afont%2Dweight%3A%20bold%3B%0A%7D%0Aul%20%7B%0Amargin%2Dtop%3A%200%3B%0A%7D%0Aul%20li%20%7B%0Alist%2Dstyle%3A%20circle%20outside%3B%0A%7D%0Aul%20ul%20%7B%0Amargin%2Dbottom%3A%200%3B%0A%7D%0Apre%2C%20code%20%7B%0Abackground%2Dcolor%3A%20%23f5f5f5%3B%0Aborder%2Dradius%3A%203px%3B%0Acolor%3A%20%23333%3B%0A%7D%0Apre%20%7B%0Aoverflow%2Dx%3A%20auto%3B%0Aborder%2Dradius%3A%203px%3B%0Amargin%3A%205px%200%2010px%200%3B%0Apadding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0Abackground%2Dcolor%3A%20white%3B%0Aborder%3A%20%23f5f5f5%201px%20solid%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20code%20%7B%0Acolor%3A%20%23444%3B%0Abackground%2Dcolor%3A%20white%3B%0A%7D%0Acode%20%7B%0Afont%2Dfamily%3A%20monospace%3B%0Afont%2Dsize%3A%2090%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0Apadding%3A%202px%204px%3B%0Acolor%3A%20%23d14%3B%0Aborder%3A%201px%20solid%20%23e1e1e8%3B%0Awhite%2Dspace%3A%20inherit%3B%0A%7D%0Adiv%2Efigure%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0Atable%20%3E%20caption%2C%20div%2Efigure%20p%2Ecaption%20%7B%0Afont%2Dstyle%3A%20italic%3B%0A%7D%0Atable%20%3E%20caption%20span%2C%20div%2Efigure%20p%2Ecaption%20span%20%7B%0Afont%2Dstyle%3A%20normal%3B%0Afont%2Dweight%3A%20bold%3B%0A%7D%0Ap%20%7B%0Amargin%3A%200%200%2010px%3B%0A%7D%0Atable%3Anot%28%5Bclass%5D%29%20%7B%0Amargin%3A%20auto%20auto%2010px%20auto%3B%0A%7D%0Aimg%3Anot%28%5Bclass%5D%29%20%7B%0Abackground%2Dcolor%3A%20%23FFFFFF%3B%0Apadding%3A%202px%3B%0Aborder%2Dradius%3A%203px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Amargin%3A%200%205px%3B%0Amax%2Dwidth%3A%20100%25%3B%0A%7D%0Ah1%20%7B%0Amargin%2Dtop%3A%200%3B%0Afont%2Dsize%3A%2035px%3B%0Aline%2Dheight%3
</head>
<body>
<h1 class="title toc-ignore">(Unofficial) overview of gtable</h1>
<h4 class="author"><em>Baptiste Auguie</em></h4>
<h4 class="date"><em>2017-09-09</em></h4>
<div id="TOC">
<ul>
<li><a href="#constructing-a-gtable">Constructing a gtable</a></li>
<li><a href="#components-of-a-gtable">Components of a gtable</a></li>
<li><a href="#modifying-a-gtable">Modifying a gtable</a></li>
</ul>
</div>
<p>The R package <code>gtable</code> is designed to help construct and manipulate layouts containing graphical elements. The standard <code>grid</code> package in R provides low-level functions to define viewports, and place graphical elements (grobs) at specific locations within the device window. <code>gtable</code> builds upon these functions and provides a higher-level interface, where one can e.g. merge two layouts, add columns, rows, insert graphical elements in a given cell, and change the display order, among other things.</p>
<p>The <code>gtable</code> package is used internally by <code>ggplot2</code>, and can therefore be used to modify the layout of such plots.</p>
<div id="constructing-a-gtable" class="section level3">
<h3>Constructing a gtable</h3>
<p>A <code>gtable</code> object can be constructed in a variety of ways,</p>
<ul>
<li>Empty table</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">gtable</span>(<span class="kw">unit</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">3</span>, <span class="kw">c</span>(<span class="st">&quot;cm&quot;</span>)), <span class="kw">unit</span>(<span class="dv">5</span>, <span class="st">&quot;cm&quot;</span>))</code></pre></div>
<p>This is an empty table with 3 rows and one column. <code>gtable_col</code> and <code>gtable_row</code> provide a simplified interface for 1 column or 1 row layouts, respectively.</p>
<ul>
<li>matrix layout of grobs</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">a &lt;-<span class="st"> </span><span class="kw">rectGrob</span>(<span class="dt">gp =</span> <span class="kw">gpar</span>(<span class="dt">fill =</span> <span class="st">&quot;red&quot;</span>))
b &lt;-<span class="st"> </span><span class="kw">grobTree</span>(<span class="kw">rectGrob</span>(), <span class="kw">textGrob</span>(<span class="st">&quot;new</span><span class="ch">\n</span><span class="st">cell&quot;</span>))
c &lt;-<span class="st"> </span><span class="kw">ggplotGrob</span>(<span class="kw">qplot</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">10</span>,<span class="dv">1</span><span class="op">:</span><span class="dv">10</span>))
d &lt;-<span class="st"> </span><span class="kw">linesGrob</span>()
mat &lt;-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">list</span>(a, b, c, d), <span class="dt">nrow =</span> <span class="dv">2</span>)
g &lt;-<span class="st"> </span><span class="kw">gtable_matrix</span>(<span class="dt">name =</span> <span class="st">&quot;demo&quot;</span>, <span class="dt">grobs =</span> mat,
<span class="dt">widths =</span> <span class="kw">unit</span>(<span class="kw">c</span>(<span class="dv">2</span>, <span class="dv">4</span>), <span class="st">&quot;cm&quot;</span>),
<span class="dt">heights =</span> <span class="kw">unit</span>(<span class="kw">c</span>(<span class="dv">2</span>, <span class="dv">5</span>), <span class="kw">c</span>(<span class="st">&quot;in&quot;</span>, <span class="st">&quot;lines&quot;</span>)))
g</code></pre></div>
<pre><code>## TableGrob (2 x 2) &quot;demo&quot;: 4 grobs
## z cells name grob
## 1 1 (1-1,1-1) demo rect[GRID.rect.583]
## 2 2 (2-2,1-1) demo gTree[GRID.gTree.584]
## 3 3 (1-1,2-2) demo gtable[layout]
## 4 4 (2-2,2-2) demo lines[GRID.lines.629]</code></pre>
<p>Actual drawing of the gtable on a graphics device is performed with <code>grid.draw()</code>; note that <code>plot()</code> is only defined for debugging purposes, it adds a light grey background and thin grid lines to help visualise the scene in its drawing context.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plot</span>(g)</code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkAAAAJACAYAAABlmtk2AAAEDWlDQ1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A0Z5a5rozDKjf33rNQyrHCZ2yLkZiYGIqbma7VPkPghoszhmkDmYyfyUEPb7TPmKgfrM+4buHChR5y93xI95D6+2Hi+WpnH6UAcnb7s/Yk4JcA9pAKxFJTU1VyLXTcr9X7Xel07udd3+s03vJCWiP57d69WxYsWOCadcbrcuXKBSU2zLzZZzjz3xeY44SH2UafMxONRM7ocT137lxmR/28w7503AfMDyQPpzkE5gEKD5EACQRH4OrVq3LhwgV1MX65ezL8OoYhTpA/wxwgmLe8cE7np8vFMRoJRCsB9P4Favp/INDrnJ5e9QCh69JK0/FD8GskHL9yvNVFx03Q3ZTe0vE4CZBAcATwP4ZhBSyZ9yZw9HEjQw/6ZuBrZ22dn6/eDawYQ9BETwY/8AvaqOE7Cz1S+KWOuCxmGbjpGxmG9MwUdPjOxRCJ2T5jmE6L03D5jCCaRufDGGkLV5/R04LPqlmGYUxM9EccrFB2sL/hhhsEqymNWEpKigoMis8wdqIPxey6J+PzabUOASclgIx8EYUC1du1Wgh5O8/jJEAC0UcAQ2aYj+Ntmbs+rsWNrxrq4bczZ854TWYkPwgWX8NoBw8e9Jq/+wn9Qwrix8wbM3rPtJmdt/7RZ3a+rj7jtZk8wuWzq2g122ez2q99+/by6quv6ux8Pj/88MOCeri2hc8LPJzU
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">grid.newpage</span>()
<span class="kw">grid.draw</span>(g)</code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkAAAAJACAYAAABlmtk2AAAEDWlDQ1BJQ0MgUHJvZmlsZQAAOI2NVV1oHFUUPrtzZyMkzlNsNIV0qD8NJQ2TVjShtLp/3d02bpZJNtoi6GT27s6Yyc44M7v9oU9FUHwx6psUxL+3gCAo9Q/bPrQvlQol2tQgKD60+INQ6Ium65k7M5lpurHeZe58853vnnvuuWfvBei5qliWkRQBFpquLRcy4nOHj4g9K5CEh6AXBqFXUR0rXalMAjZPC3e1W99Dwntf2dXd/p+t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ltBH5kPFuBY2+3cerA+DEKtrG2VF6p3Q58Y00kTAyWth/4FbTdpr4H2SOlf6jovi1NJ3awPMr2l52RAp3M26S0Sp+ust+ms4DRaddZJNO2Oh7FzLXzrrD0t+rPqVNLbmDrQX+fBys4K9uecc47o05R2UqlSpczEoPozrCvRk+wLEADZtyInAgjYENBbZjoeJ9hj7tZ2K7gJVaR1++3gwYNBs9kpTwOWULfRtm3bFrR8/x3WBHQa/Dj5way9Z1ZyumxrbIrT5frWWV876RGtOvsGrU7X2anr16VLF3niiSes4kJ+v/nmm0Xb4XstQh7AznQBboGlU/ACAQScELCCFisw8S/TCmbsDNoMV5aWHUl5/nXhPQJuFNA5r6ynJUPVTycW1QlBSVkTIADKmhtHIYBAEIGyZcuaPevXrw+Yw9p+9tlnB9zvu9EqS3uUAt1S0Mff9baO9sroWmMk
<p>The gridExtra package provides a few conventient constructor functions, e.g.</p>
<ul>
<li>based on <code>grid.arrange</code></li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">dummy_grob &lt;-<span class="st"> </span><span class="cf">function</span>(id) {
<span class="kw">grobTree</span>(<span class="kw">rectGrob</span>(<span class="dt">gp=</span><span class="kw">gpar</span>(<span class="dt">fill=</span>id, <span class="dt">alpha=</span><span class="fl">0.5</span>)), <span class="kw">textGrob</span>(id))
}
gs &lt;-<span class="st"> </span><span class="kw">lapply</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">9</span>, dummy_grob)
<span class="kw">grid.arrange</span>(<span class="dt">ncol=</span><span class="dv">4</span>, <span class="dt">grobs=</span>gs,
<span class="dt">top=</span><span class="st">&quot;top</span><span class="ch">\n</span><span class="st">label&quot;</span>, <span class="dt">bottom=</span><span class="st">&quot;bottom</span><span class="ch">\n</span><span class="st">label&quot;</span>,
<span class="dt">left=</span><span class="st">&quot;left</span><span class="ch">\n</span><span class="st">label&quot;</span>, <span class="dt">right=</span><span class="st">&quot;right</span><span class="ch">\n</span><span class="st">label&quot;</span>)
<span class="kw">grid.rect</span>(<span class="dt">gp=</span><span class="kw">gpar</span>(<span class="dt">fill=</span><span class="ot">NA</span>))</code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkAAAAJACAYAAABlmtk2AAAEDWlDQ1BJQ0MgUHJvZmlsZQAAOI2NVV1oHFUUPrtzZyMkzlNsNIV0qD8NJQ2TVjShtLp/3d02bpZJNtoi6GT27s6Yyc44M7v9oU9FUHwx6psUxL+3gCAo9Q/bPrQvlQol2tQgKD60+INQ6Ium65k7M5lpurHeZe58853vnnvuuWfvBei5qliWkRQBFpquLRcy4nOHj4g9K5CEh6AXBqFXUR0rXalMAjZPC3e1W99Dwntf2dXd/p+tt0YdFSBxH2Kz5qgLiI8B8KdVy3YBevqRHz/qWh72Yui3MUDEL3q44WPXw3M+fo1pZuQs4tOIBVVTaoiXEI/MxfhGDPsxsNZfoE1q66ro5aJim3XdoLFw72H+n23BaIXzbcOnz5mfPoTvYVz7KzUl5+FRxEuqkp9G/Ajia219thzg25abkRE/BpDc3pqvphHvRFys2weqvp+krbWKIX7nhDbzLOItiM8358pTwdirqpPFnMF2xLc1WvLyOwTAibpbmvHHcvttU57y5+XqNZrLe3lE/Pq8eUj2fXKfOe3pfOjzhJYtB/yll5SDFcSDiH+hRkH25+L+sdxKEAMZahrlSX8ukqMOWy/jXW2m6M9LDBc31B9LFuv6gVKg/0Szi3KAr1kGq1GMjU/aLbnq6/lRxc4XfJ98hTargX++DbMJBSiYMIe9Ck1YAxFkKEAG3xbYaKmDDgYyFK0UGYpfoWYXG+fAPPI6tJnNwb7ClP7IyF+D+bjOtCpkhz6CFrIa/I6sFtNl8auFXGMTP34sNwI/JhkgEtmDz14ySfaRcTIBInmKPE32kxyyE2Tv+thKbEVePDfW/byMM1Kmm0XdObS7oGD/MypMXFPXrCwOtoYjyyn7BV29/MZfsVzpLDdRtuIZnbpXzvlf+ev8MvYr/Gqk4H/kV/G3csdazLuyTMPsbFhzd1UabQbjFvDRmcWJ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dKmTZvs+++/d7s1atTIatasWeoQv4t7qQ0xK2L3ia0NitmFRQQQQKCUAAFQKRJWIIDA9gh06tQp6KquMXoStemJPa/2UcNlJR2bKGkkaQ3AmCzpPH7aa6+9/EXeEUAAgaQCBEBJediIAAKxAv50E1u2bIld7Zbr1Klju+++u1tWeyCN9ZMsaWwfP6k7e6K0fv16e+ihhxJtcus+/vhjGzNmjFvu1auXaaBGEgIIIJCOAAFQOkrsgwACTqBevXruXdNhKDiJT3fddVewSuMAacqLREnTYPgBUq1atezUU09NtJtbp9GkE80ppglZNd+Yny666CJ/kXcEEEAgpQABUEoidkAAAV+gqKjILW7evNk0jo9GYtZcXH46+OCD3QSm+jxz5kxTrczjjz/uJjZVrdHkyZPtiiuusOOOOy54RHb77bdbhw4d/FOUel+wYIHtueeepglW1WhaPcbefPNN22+//YJxfw444ADTpKgkBBBAIF0B5gJLV4r9EEDA7rvvPouvaWnevLktWrQo0FGQcv755wc1PP6G6tWrmwInP1WtWtWuvfZau+aaa/xVwbs/F5jGCurTp4+99dZbbpubv8c7LvYRnAKhl156yZQPEgIIIJCOAHOBpaPEPgggEAhceOGFdvXVV5tfE6QNGgE6tju7Bjd87rnn7KmnnnLzfekXjZIf/Kgd0cCBA+3dd99NGPy4nX/8R4/HXn/9dRs6dKipm7waVvvBT4sWLUzzf7399tsEP7FoLCOAQFoC1AClxcROCCAQLzBv3jxTWyANVui3DYrfR5+1j3pqqR1Pq1atrHPnzikDFr8GKHbWefUYmzRpks2aNcs1dtas8wqQSAgggEB5BfSHGQFQedXY
<ul>
<li>with a pre-defined layout</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">gt &lt;-<span class="st"> </span><span class="kw">arrangeGrob</span>(<span class="dt">grobs=</span>gs, <span class="dt">layout_matrix=</span><span class="kw">rbind</span>(<span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>),
<span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">1</span>,<span class="dv">4</span>,<span class="dv">5</span>),
<span class="kw">c</span>(<span class="dv">6</span>,<span class="dv">7</span>,<span class="dv">8</span>,<span class="dv">9</span>,<span class="dv">9</span>)))
<span class="kw">grid.draw</span>(gt)
<span class="kw">grid.rect</span>(<span class="dt">gp=</span><span class="kw">gpar</span>(<span class="dt">fill=</span><span class="ot">NA</span>))</code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAkAAAAJACAYAAABlmtk2AAAEDWlDQ1BJQ0MgUHJvZmlsZQAAOI2NVV1oHFUUPrtzZyMkzlNsNIV0qD8NJQ2TVjShtLp/3d02bpZJNtoi6GT27s6Yyc44M7v9oU9FUHwx6psUxL+3gCAo9Q/bPrQvlQol2tQgKD60+INQ6Ium65k7M5lpurHeZe58853vnnvuuWfvBei5qliWkRQBFpquLRcy4nOHj4g9K5CEh6AXBqFXUR0rXalMAjZPC3e1W99Dwntf2dXd/p+tt0YdFSBxH2Kz5qgLiI8B8KdVy3YBevqRHz/qWh72Yui3MUDEL3q44WPXw3M+fo1pZuQs4tOIBVVTaoiXEI/MxfhGDPsxsNZfoE1q66ro5aJim3XdoLFw72H+n23BaIXzbcOnz5mfPoTvYVz7KzUl5+FRxEuqkp9G/Ajia219thzg25abkRE/BpDc3pqvphHvRFys2weqvp+krbWKIX7nhDbzLOItiM8358pTwdirqpPFnMF2xLc1WvLyOwTAibpbmvHHcvttU57y5+XqNZrLe3lE/Pq8eUj2fXKfOe3pfOjzhJYtB/yll5SDFcSDiH+hRkH25+L+sdxKEAMZahrlSX8ukqMOWy/jXW2m6M9LDBc31B9LFuv6gVKg/0Szi3KAr1kGq1GMjU/aLbnq6/lRxc4XfJ98hTargX++DbMJBSiYMIe9Ck1YAxFkKEAG3xbYaKmDDgYyFK0UGYpfoWYXG+fAPPI6tJnNwb7ClP7IyF+D+bjOtCpkhz6CFrIa/I6sFtNl8auFXGMTP34sNwI/J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wAAQIECBDIg4AAlIcuqIEAAQIECBDIVEAAypTbZAQIECBAgEAeBASgPHRBDQQIECBAgECmAgJQptwmI0CAAAECBPIgIADloQtqIECAAAECBDIVEIAy5TYZAQIECBAgkAcBASgPXVADAQIECBAgkKmAAJQpt8kIECBAgACBPAgIQHnoghoIECBAgACBTAUEoEy5TUaAAAECBAjkQUAAykMX1ECAAAECBAhkKiAAZcptMgIECBAgQCAPAgJQHrqgBgIECBAgQCBTAQEoU26TESBAgAABAnkQ6JyHItRAgAABAsUSuPX55+LqP4+N8TNmxPiZM6Jrp04xvP+A2Hr99eP0XXeLHQcOLNYBrcXVzpr+Ttz90/vjlTGvxtQJ02PRgkXRd1CfGLHvlrH7SbvGBkMHlFJHACplWx0UAQIE0hF4cebMOP2W/4k/vvjieyaYNnt23P/KxPjlE4/HGbuNjPP2/2is07Xre7bzQH4EHv7vx+Kmf/tDzH9n/gpFvf78tEi+HrpiVJxy8QnVMLTCBiX4QQAqQRMdAgECBLIQmLdwYRx9zX/HuKmvV6dbf5114hPbf6D6qs+cynNPTJkc/115Vaht8eL48SMPx5vz5sXlRx+bRWnm+BsEnv7jc3Hd12+q7bnj4dvFFrtvFp27do5JT/4lHrnm8Vgwd2H88tNXxYk/OCZ2PnqH2rZlWBGAytBFx0CAAIEMBL56x+218HPg0GFx1bHHR7+ePVeY+aw99oyDrvhVTK28GnTFk2PisK2Gx3HbbrfCNn5ovcCcN+bEVf9wfbWQDh06xKcvPSm2PWB4rbCRJ+wcu39i1/jJ0b+IhfMWxtVn3RCb7zYk1hvUt7ZN0VecBF30DqqfAAECGQgsamuLy8eMrs7Ut3v36is77w4/yZMf2GhgXHT4kbWKLh39eG3dSn4Ext72TMybtfRtr70/PXKF8NNe5eDtBsb+X/xQ9cfFbYtj1HVj2p8qxXcBqBRtdBAECBBIV+CxyZMjeZsr
</div>
<div id="components-of-a-gtable" class="section level3">
<h3>Components of a gtable</h3>
<p>Lets have a closer look at the gtable we created earlier.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">print</span>(g)</code></pre></div>
<pre><code>## TableGrob (2 x 2) &quot;demo&quot;: 4 grobs
## z cells name grob
## 1 1 (1-1,1-1) demo rect[GRID.rect.583]
## 2 2 (2-2,1-1) demo gTree[GRID.gTree.584]
## 3 3 (1-1,2-2) demo gtable[layout]
## 4 4 (2-2,2-2) demo lines[GRID.lines.629]</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">names</span>(g)</code></pre></div>
<pre><code>## [1] &quot;grobs&quot; &quot;layout&quot; &quot;widths&quot; &quot;heights&quot;
## [5] &quot;respect&quot; &quot;rownames&quot; &quot;colnames&quot; &quot;name&quot;
## [9] &quot;gp&quot; &quot;vp&quot; &quot;children&quot; &quot;childrenOrder&quot;</code></pre>
<p>Other useful characteristics of the gtable are,</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">length</span>(g); <span class="kw">nrow</span>(g); <span class="kw">ncol</span>(g)</code></pre></div>
<pre><code>## [1] 4</code></pre>
<pre><code>## [1] 2</code></pre>
<pre><code>## [1] 2</code></pre>
<p>where we note the dual nature of a gtable: it looks like a matrix, in the sense that it defines a rectangular table of nrow x ncol cells, but its also a <em>list</em> with an arbitrary length, defining where and how many grobs are to be placed in this tabular layout.</p>
<p>The most important components are,</p>
<ul>
<li><code>grobs</code>: This is a list of grobs of <code>length(g)</code>. Grobs are placed in the tabular layout defined by the gtable, and multiple grobs can overlap and/or be stacked in the same cell(s).</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">length</span>(g<span class="op">$</span>grobs)</code></pre></div>
<pre><code>## [1] 4</code></pre>
<ul>
<li><code>layout</code>: this is a data.frame indicating the position of each grob.</li>
</ul>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">g<span class="op">$</span>layout</code></pre></div>
<pre><code>## t l b r z clip name
## 1 1 1 1 1 1 on demo
## 2 2 1 2 1 2 on demo
## 3 1 2 1 2 3 on demo
## 4 2 2 2 2 4 on demo</code></pre>
<p>The z-column is used to define the drawing order of the grobs, which becomes relevant when multiple grobs are stacked on top of each others. * <code>widths</code> and <code>heights</code>: this is the size description of the cells, given as grid units.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">g<span class="op">$</span>widths; g<span class="op">$</span>heights</code></pre></div>
<pre><code>## [1] 2cm 4cm</code></pre>
<pre><code>## [1] 2in 5lines</code></pre>
</div>
<div id="modifying-a-gtable" class="section level3">
<h3>Modifying a gtable</h3>
<p>The gtable package defines several high-level functions to operate on a gtable object,</p>
<ul>
<li><p><code>t.gtable</code> to transpose the layout (future versions may support more general rotations)</p></li>
<li><p><code>[.gtable*</code>, <code>gtable_filter</code>, <code>gtable_trim</code> to extract a portion of the gtable</p></li>
<li><p><code>cbind.gtable*</code>, <code>rbind.gtable*</code> to combine 2 gtable objects (particularly useful for aligning multiple ggplots)</p></li>
<li><p><code>gtable_add_cols</code>, <code>gtable_add_rows</code>,<code>gtable_add_col_space</code>, <code>gtable_add_row_space</code>, <code>gtable_add_padding</code>, <code>gtable_col_spacer</code>, <code>gtable_row_spacer</code></p></li>
</ul>
<p>Manual operations at the low-level on the gtable can involve the grobs or the layout, but care should be taken to keep the two consistent (e.g. make sure that the length of both are in sync).</p>
<div id="examples-to-alter-ggplot2-plots-with-gtable" class="section level4">
<h4>Examples to alter ggplot2 plots with gtable</h4>
<p>The <code>gtable</code> tag on Stack Overlfow has several real-life examples using gtable to alter a ggplot2 before drawing.</p>
<ul>
<li><p>aligning (multiple) <a href="http://stackoverflow.com/a/17768224/471093">ggplot objects on a device</a>, another <a href="http://stackoverflow.com/a/16798372/471093">use-case scenario</a>, aligning <a href="http://stackoverflow.com/a/14233531/471093">base plot and ggplot</a></p></li>
<li><p>adding new grobs <a href="http://stackoverflow.com/a/17371177/471093">aligned with the plot panel</a>, also <a href="http://stackoverflow.com/a/17493256/471093">this one</a></p></li>
<li><p>calculate <a href="http://stackoverflow.com/a/16442029/471093">the device size based on the plot aspect ratio</a></p></li>
<li><p>add <a href="http://stackoverflow.com/a/17661337/471093">new axes to a facet_grid layout</a>, or a <a href="http://stackoverflow.com/a/18511024/471093">second axis</a></p></li>
</ul>
</div>
</div>
<script type="text/javascript">
window.onload = function() {
var i, fig = 1, caps = document.getElementsByClassName('caption');
for (i = 0; i < caps.length; i++) {
var cap = caps[i];
if (cap.parentElement.className !== 'figure' || cap.nodeName !== 'P')
continue;
cap.innerHTML = '<span>Figure ' + fig + ':</span> ' + cap.innerHTML;
fig++;
}
fig = 1;
caps = document.getElementsByTagName('caption');
for (i = 0; i < caps.length; i++) {
var cap = caps[i];
if (cap.parentElement.nodeName !== 'TABLE') continue;
cap.innerHTML = '<span>Table ' + fig + ':</span> ' + cap.innerHTML;
fig++;
}
}
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>