---
title: "Groups and Paths and Masks in R Graphics"
author: "Paul Murrell"
date: 2021-12-06
categories: ["Internals"]
tags: ["graphics"]
---

```{r eval=FALSE, echo=FALSE}
knitr::opts_chunk$set(
  fig.width = 2, fig.height = 2
)
```

Support for gradient fills, pattern fills, clipping paths and masks
was added to the R graphics engine 
[in R version 4.1.0](https://developer.r-project.org/Blog/public/2020/07/15/new-features-in-the-r-graphics-engine/).

The development version of R (likely to become R version 4.2.0)
contains support for several more graphical tools:
groups, compositing operators, and affine transformations, plus
some tweaks to paths and masks.

An R-level interface for these new features has been added to the
'grid' graphics package.  

```{r eval=FALSE}
library(grid)
```

The following code demonstrates drawing a group with
the new `grid.group()` function.  The basic idea
is that we can draw a group of shapes in isolation and then
add the result to the main image.  In this case, we draw a 
rectangle and a circle
as an independent group before adding them to the image.

One of the advantages of drawing groups in isolation is that we
can combine shapes using different compositing operators.
In this case, we use a "clear" operator, which means that,
rather than drawing the rectangle on top of the circle, the 
rectangle creates a hole in the circle.

A green line was drawn first to show that there is a hole
in the circle, through which we can see the
green line.

```{r eval=FALSE, composite}
grid.segments(gp=gpar(col=3, lwd=50))
grid.group(rectGrob(width=.4, height=.2),
           "clear",
           circleGrob(r=.4, gp=gpar(col=NA, fill=4)))
```

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAAGACAMAAACTGUWNAAAATlBMVEUil+Yqn9Eqn9I7rqo9r6w+r60/pelApelRwnRVxG1ZxnNaxnRh0E9n0lZo0ldqvN5rvd96wfCy4NW14djI5fnT6vrW89HX89PZ9NX///87c0R0AAAACXBIWXMAAB2HAAAdhwGP5fFlAAAJ1UlEQVR4nO3d6XIbNxSEUWW3E8nZbEd8/xeNRcoSRc2Cpfv2HaD7AWziO6ip8h/j7uQ17fHTPWR36oMcdKj+BmgbrL8BmvbfA6q/AVqGu/8GaBmyvwHqB/z+GKBh0PtvgOqB+xugctjvjwFqh77/Bqgbvr8Bagb//higaoT7b4CKUfoboHiM748Byse5/wYoHau/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UC9AICd95fVBbTr7V8AsPv+tbqBct399wH23x9XRxCuv/8uQMn77+oMuvEBSvrPKwDovwNQ1n9WAUT/bYDS/gbgABT3n1MA0n8LoKL/jAKY/hsAVf3nEwD1Xweo7D+bAKr/KkB1fwNAAer7zyUA678C0NJ/JgFc/2WAtv7zCAD7LwK09p9FANl/CaC9/xwC0P4LAD39ZxDA9n8P0NffAL0Anf3HFwD3vwXo7j+6ALr/DQCg/9gC8P5vASD9RxbA938DAOo/rgCh/zUArP+oAoz+VwDA/mMKUPq/AkD7jyjA6f8CAO4/HAEp/wsAvv9YArT+zwCM/iMJ8PpfADj9xxEg9j8DsPqPIsDs/wTA6z+GALX/NwBm/wEIuPm/AZD7H12A3f9090Duf2wBev/THb3/kQX4/UMAjkoQkD8K4JACIf2jAI4nEJM/DuBoBFH9AwEOJRDWPxLgOARx+YMBDiIQ2T8Y4AgEofnjAbITBOdXAKQWCO+vAMhLEJ9fBJCTQJFfBpCPQJNfCJCLQJVfCpCHQJdfC/DwNcWDZMr8UoBv/U/6N+G0+ZUAl/4nLYG0/eX08v4nHYEu++tUANf9z79jyvonGcBt/1M0gSD18jQAC/3PP2a2+icRwEr/8++Zqv5JA7DR//yT5ql/kgDs9D//qknqnxQABf0vv2z8+E8LByjtf/l1Y8d/WjRAVf/LLxw3/tOCAer7f/+d46W/LBaguf/Lzx2n/PNCAbr73/74QyZ/u0gAdP8hFgjg/kuLA3j4T33WlAsDcP/lRQH4+7OyIAD3X1sMgL8/qwsB8P1fXwSA7//GAgB8/7fGB3D/zdEB/P3ZHhvA939nZADf/71xAXz/d0cFcP/9MQH8/SkYEcD3v2Q8APcvGg3A35+ysQB8/wtHAnD/0nEA/P0pHgXA9798DAD3rxgBwN+fmuEBfP+rBgdw/7qhAfz9qRwYwPe/dlgA968eFMDfn/ohAXz/GwYEcP+W4QD8/WkaDMD3v20oAPdvHAjA/Vv3P5+k4iAdZ0HVAAAAAElFTkSuQmCC" width="192">

The following code demonstrates the new path-drawing facilities,
which includes the new function `grid.fill()` to fill a path.
A path can be created from any number of shapes and then we can 
stroke or fill the path (or both).  In this case, we describe
a path based on a rectangle and a circle.

When a path consists of overlapping shapes, the "inside" of the
path - the area that gets filled - can become complex.
We can control the "rule" that is used to decide the filled area.
In this case, we use the "even-odd" rule, which means that the
area inside the rectangle is actually outside the path;
the result again is a hole in the circle.

The path is filled with a blue colour (and no border).

```{r eval=FALSE}
grid.segments(gp=gpar(col=3, lwd=50))
path <- gTree(children=gList(circleGrob(r=.4), 
                             rectGrob(width=.4, height=.2)))
grid.fill(path,
          rule="evenodd",
          gp=gpar(col=NA, fill=4))
```

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAAGACAMAAACTGUWNAAAAGFBMVEUil+Zh0E9n0lZo0lfW89HX89PZ9NX///8qWmkBAAAACXBIWXMAAB2HAAAdhwGP5fFlAAAJpklEQVR4nO3da3LbSBSDUY+TmPvf8cSSY8syyX4BF5fdwAJG7O+wWJX54X7ZvK69/foPshf1QS46VH8D9A3W3wBd+/OK6m+AnuHefwP0DNnfAO0Dfn8M0DHo+2+A5oH7G6Bx2O+PAVqHfv8N0DZ8fwO0DP79MUDTCO+/ARpG6W+A6jG+PwaoH+f9N0DtWP0NUDfS98cAlaO9/waoGrG/ASrG+/4YoGbM998A5XH7G6A06vfHAMWR338DFMZ+/w1wPvr7b4DTBfQ3wMn43x8DnC3i/TfA8ULefwMcLub9N8DRovobYH9B3x8DHCyuvwH2FtjfADsL+/5fHuDlY9j/amj/qwG8VGzwJ2L7XwagpjzEIbj/BQDa048wRPdPDjAWvwMhvH9iAEz8NoT4/kkBsPGrEQT9MwJw6lcYKPqnA2DWLxhI+ucC4Nc/MdD0TwQQVf/AQNQ/DUBs/p8Eqv45AOLrPxvI+mcAUOV/IND11wMo8/8jEPYXA6jr3ybtrwVQp/83YX8lgDr74xYEUCd/3mIA6tx7WwhAnfpoiwCoM59tBQB14/NND6AOXN7UAOq4dZsXQF22dpMCqLO2bEYAddO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+gQCXEgjrHwlwHYK4/MEAFxGI7B8McAWC0PzxANkJgvMrAFILhPdXAOQliM8vAshJoMgvA8hHoMkvBMhFoMovBchDoMuvBXj9neJCMmV+KcDf/pv+TjhtfiXAvf+mJZC2v59e3n/TEeiyf00F8Nj/9hxL1t9kAM/9t2gCQer9aQB2+t8eZrX6mwjgoP/teZaqv2kATvrfHmmd+psEoND/9lSL1N8UABX97082f/z3hQPU9r8/3dzx3xcN0NT//oTzxn9fMEB7/3/POV/6+2IBuvt/Pu485T8WCjDc//nhL5n8+yIB0P2nWCCA++8tDuD1j/qsKRcG4P77iwLw9+dgQQDuf7QYAH9/DhcC4Pf/eBEAfv9PFgDg9/9sfAD3Px0dwN+f87EB/P4XRgbw+18aF8Dvf3FUAPcvjwng70/FiAB+/2vGA3D/qtEA/P2pGwvA73/lSADuXzsOgL8/1aMA+P2vHwPA/RtGAPD3p2V4AL//TYMDuH/b0AD+/jQODOD3v3VYAPdvHhTA35/2IQH8/ncMCOD+PcMB+PvTNRiA3/++oQDcv3MgAPfv3f9IyHJKheUGVAAAAABJRU5ErkJggg==" width="192">

The following code demonstrates the new luminance mask support,
which is available via the `as.mask()` function.
The `as.mask()` function creates a mask from a grob and a 
`type`.  

The `type` can be `"luminance"`, which means that the
luminance of the grob determines the semitransparency of the
masked output.  In this case, we define a mask based on a 
white circle with a black rectangle drawn on top.  

When we push a viewport with a luminance mask, any subsequent drawing
will be opaque where the mask is white and transparent where the 
mask is black (and semitransparent where the mask is grey).
In this case, having pushed a viewport with the mask,
we fill the entire image with blue and the result is a blue
circle (because the circle in the mask is white) with a hole
(because the rectangle in the mask is black).

```{r eval=FALSE, luminance, fig.keep="none"}
pdf("luminance-mask.pdf", width=2, height=2)
grid.segments(gp=gpar(col=3, lwd=50))
mask <- gTree(children=gList(circleGrob(r=.4, 
                                        gp=gpar(col=NA, fill="white")), 
                             rectGrob(width=.4, height=.2,
                                      gp=gpar(col=NA, fill="black"))))
pushViewport(viewport(mask=as.mask(mask, "luminance")))
grid.rect(gp=gpar(fill=4))
dev.off()
```
```{r eval=FALSE, echo=FALSE}
system("pdftoppm -png -rx 96 -ry 96 luminance-mask.pdf > luminance-mask.png")
```

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMAAAADACAIAAADdvvtQAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAL10lEQVR42u3db2gUZx4H8GdmdrLZTdb8WaM2SY1t4iXrtUa5xD9c9UK8FyUYrnAUhVrECkdbpJdCheKVK1dsKVg4W6QtB+nhtUKLlCsooW8acnrFv1Djna45TTFxk5PErEk22c1mdmfuRY5t1ESTfZ555nlmv99XgmSz2d+H5/ebmZ15FMuyCOKixFLjf+z5/VjqLp9fp+IThx4AQpzRA0DQA0CIc3oACHoACHqc0wNA0ANA0ONoAAh6AAh6AAiRUQ8AQQ8AQQ8AIZLqASDoASDoASBEUj0ABD208eR4kSKx9KRh9YwYhJBLQ0Y0YWb+68SNRObfrTW+zL9Lfeq6ZTohpDaoF+hKZUDLWT2EECWnvhMdT1nhO8ZALN3Zlzw7mByOm0xetsyvbir3Nld5KwJaaKnu9yg5oicnAMVT1vnB6VO3kh29CVZiHumppdq39XHvhvK8LDBJpMfNgCKxdGff1FfhxLURw8G3URfUd4R8zVX5C+x0culxIaBowjzZm3DczXyStlf7Sn2qa/S4B1DaJKcjyUPnYqK5mVPS/o2BLZVeTZVejxsAxVNWe/fk4Qsx6d55W2Ngb33BzJAkqR65AUVi6Q/OxWYfbMuY1hrfKw3k04HXZdQjKyB30LnnwC144cmV3+TnRwHI9oZ1oGvMTXTuY1Rb/YWmTQOQLWPysauTf/rnOHF7alZ9Wb7itKKYAMQsPdHU7pMjfE4DihBdH1sb+qiwYBCA0LNc3tGEBtTVn3yzazR3Fp45l6La6s+DJVcAaNETz7tnxo/+a5IghFSs6Kxe9bWYU5GIgKIJs+X4cC4vPHMuRQ31B/P0CQB6dNva2xGFmDnzVN0RodpZkadEIEBpk3z8w4SMFyV4pqryRFXltyK0sy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J/4pXLTkzEUWv0ZDsrsOOw6vK1Y4/68OJWnnsxA3VB/EIbY6ul4vsyR/ZNUznpgyE165gDEbVdbGHKBnvsBcd4TGYZk13MPIEd21IYhqfWQzFNand2PfdooDF9/aXQsBBYLP2I/vK1YhF1HFcuynNUzE8tSL4f3wdAC9fA/3zMvoHFjzHE9GUN9kWf7Iq0g8pC0NQZeXV+oCbM/gJbYcVsEPYQQRbGKi64HCm8O3dkAKHOmvaV0R8ivirRVpCqInkyCJVc2N+zHWP3gyHxh93KeV0mzOYwXJHn6xOZfHMD3GDPZ/XTB97uWizAyPxiPmB+Zopg1TxwvKb7a0/uiYRTlLJ0yv/p+U7GAC89PM9D6l+uEfXN+33D58lOJqaXxREUO6mmt8f2tNbi6xCPym1T2XPqN+B/lxGT55fBrubMUlfnVo9uDtt7PlVuAZg7yB29vuXFzp+v1vP3MkhfWFGiSbOTnkeVjVRSz4rF/rFh2pqd31/BIo1t71ntNRXbfyZWjK9DsTE2V/tj/nJsYtdb43tgYqJRwE2OPjB93fn5085q/v1yx9ZOL5MSNBOhgBVpcZu9LGk9Z7d2Thy/EpPsr2hoDe+sL5GpYbgA05662aZOcjiQPnYtdGzEEf/91QX3/xsCWSq/miv2uPS7QQwjRVNK00tu00htNmCd7E1+FE6JJqgvqO0K+7dU+MU8o58QKtKgdtSOxdGfflOOSZtw0V+XLO+W4BFDW+7HHU9b5welTt5IdvYnhuMnhrZb51ZZq39bHvRvK82QfcVwCKGs9D2IK3zEGYunOvuTZwSQrT2V+dVO5t7nKWxHQQkt116ORDBArPfN1uknD6hkxCCGXhoxo4idSs08QzN4DpNSnrlumE0Jqg3qBrri1N7kEkK16EPqo0IO4ExD0ABD0ABD0IHIBgh4Agh4Agh5ELkDQA0DQA0DQg8gFCHoACHoACHoQuQBBDwBBD+IEIOgBIOhBnAAEPa7M/wCqt8W33Q2ZoAAAAABJRU5ErkJggg==" alt="example of a luminance mask">

The remaining examples demonstrate affine transformations, using the
`grid.define()` function and the `grid.use()` function.  If we
define a group (without drawing it) in one viewport and then use the group in a 
different viewport, the group is transformed based on differences
in location, size, and rotation of the two viewports.  In this
case, we define a group based on a circle and a rectangle (using the
"clear" operator so that the rectangle creates a hole in the circle), 
in a viewport
that is the full size of the image, then we push a viewport that
is only one-third the height of the image and use the group
that we defined.

This produces a vertically squashed version of the group because the
viewport we are using the group in is much shorter than the viewport
that the group was defined in.

```{r eval=FALSE, transform-group}
grob <- groupGrob(rectGrob(width=.4, height=.2),
                  "clear",
                  circleGrob(r=.4, gp=gpar(col=NA, fill=4)))
grid.define(grob, name="donut")
grid.segments(gp=gpar(col=3, lwd=50))
pushViewport(viewport(height=1/3))
grid.use("donut")
popViewport()
```

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAAGACAMAAACTGUWNAAAAolBMVEUil+YimOUjl+YjmOYkmd8lmt8qn9MqoNIrm+crn9MroNMsnOc3qrQ4qrQ5qrc6o+Q6q7Y7pOQ/pepApupLvYFMvYFTxHBVxG1ZxnNaxnReteBetuBftt9gtuFhz1Bh0E9n0lZo0ldrue5suu9tuu5tuu+y4NW03Pa03Pe13Pa14di23ffO6PnP6PrT6vrW89HX89PZ9NX+/v7+/v/+///////uNQJqAAAACXBIWXMAAB2HAAAdhwGP5fFlAAAKGElEQVR4nO2d2XbbVgxF1apznc5KOttp6qbu4CRN9P+/VmuyJlK8w8EFcHH2o5dXlrQ3CNKOSc2WpIg3z68hzLTfiFNQ/hmgDJh/Biji9Q3KPwOUgJt/BigB6Z8B8gHuHwYoADr/DJAN2D8DZILdPwyQC3r+GSAPvH8GyAG+fxggC4H5Z4AMRPwzQDIS+4cB0pGZfwZIRco/A6QhtH8YIBGx+WeAJAT9M0ACcvuHAVKQnH8GmEbWPwNMIbp/GGAS4flngAmk558BLiM+/wxwkQb+GeAC8vuHAS7RYv4ZYJwm888Ao7SZfwYYo5V/Bhim0f5hgBHa+WeAIRr6Z4ABmu1/BhikqX8GOKOtfwY4pbF/BjihtX8GOKa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Sef7LYcvzVgftPDQMJYPf5M/ZBBKD/CgAB6L+G+gD0X0V1APqvozYA/VdSGYD+a6kLQP/VVAWg/3pqAtA/gIoA9I+gPAD9QygOQP8YSgPQP4jCAPSPoiwA/cMoCkD/OEoC0D+QggD0jyQ/AP1DyQ5A/1hyA9A/mMwA9I8mLwD9w8kKQP94cgLQvwAZAehfgvQA9C9CcgD6lyE1AP0LkRiA/qVIC0D/YiQFoH85UgLQvyAJAehfkukA9C/KZAD6l2UqAP0LMxGA/qW5HID+xbkYgP7luRSA/htwIQD9t2A8AP03YTQA/bdhLAD9N2IkAP23YjgA/TdjMAD9t2MoAP03ZCAA/bfkPAD9N+UsAP235TQA/TfmJAD9t+Y4AP035ygA/bfnMAD9K3AQgP412AegfxUeA9C/DrsA9K/ENgD9a7EJQP9qrAPQvx6rAPSvyIz+dZnRvy4z+tdldqMt/YCb19o62jPTln5AwPk3FSCkf0MBYvq3EyCofzMBovq3EiCsfyMB4vq3ESCwfxMBIvu3ECC0fwMBYvvXDxDcv3qA6P61A4T3rxyA/nUD0L9uAPpfqgag/xV6Aeh/jVoA+t+gFYD+tygFoP8dOgHo/xGVAPS/RyMA/R+gEID+D2kfgP6PaB6A/o9pHYD+T2gcgP5PaRuA/s9oGoD+z2kZgP4HaBiA/odoFyDi/UcJNAtA/8O0CsD9M0KjAPQ/RpsA3D+jNAnA+R+nRQDO/wUaBOD8X0I+AP1fRDwA989lpANw/icQDsD5n0I2AOd/EtEA9D+NZADunwQEA3D+U5ALQP9JiAXg/klDKgDnPxGhAPSfikwA7p9kRAJw/tORCED/GQgE4P7JAR+A858FPAD954EOwP2TCTgA5z8XbAD6zwYagPsnH2QAzn8BwAD0XwIuAPdPEbAAnP8yUAHovxBQAPov5X/VDViORsYXKQAAAABJRU5ErkJggg==" width="192">

The following code is similar, but this time we use the group in
a viewport that is one-third the width of the image, so the group
is horizontally squashed.

Another difference in this example is that the group is based on 
a path that is filled using the "even-odd" rule, which demonstrates
that we can combine these new features of groups and paths.

```{r eval=FALSE, transform-fill}
grid.newpage()
grob <- fillGrob(path,
                 rule="evenodd",
                 gp=gpar(col=NA, fill=4))
grid.define(grob, name="donut")
grid.segments(gp=gpar(col=3, lwd=50))
pushViewport(viewport(width=1/3))
grid.use("donut")
popViewport()
```

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAAGACAMAAACTGUWNAAAAY1BMVEUil+YimOUjl+YjmOY3qrQ4qrQ5qrc6q7ZLvYFLvYJMvYFeteBetuBftt9gtuFhz1Bh0E9n0lZo0ldrue5suu9tuu5tuu+03Pa03Pe13Pa23ffW89HX89PZ9NX+/v/+//////8umZ8XAAAACXBIWXMAAB2HAAAdhwGP5fFlAAALzklEQVR4nO2c2WIcRRRDG8K+2gaSQOJk/v8r8Xgde7q6a9G9UtnSC28TfE6XVD3BLAenK9dXF5As7B9k0qD4W0BfYPwtoCufL1H8LaAnuOffAnqC5G8B7QH2jwV0BPr8W0BzwPwtoDHY/rGA1qCffwtoC56/BbQE3j8W0JSA598CGhLC3wKqE9E/FlCfmOffAmoTxd8C6hLUPxZQmbDn3wKqEsjfAioS1z8WUJPI598C9hPL3wL2Eto/FrCb4OffAnYS/fxbwHbCn38L2EwCfwvYSHz/WMBWMp5/Cygn5fm3gGJynn8LKCWLvwWsJ6l/LKCQPP4WsJZE/hawkrT+fwUCPi7Lv+jPTOU/sYCvy1PeAT83l/+0Aj4uL4I6CMn8JxVwhh+mIJv/nAL+WuO/LO/HPzmd/4wCvq7jR0xBPv8JBazWD6aGCPznE7DJf8wAg/90Anb4jxig8J9NwC7/fgMc/pMJqODfa4DEfy4B5fvPabruQiz+Uwmo499lgMZ/KgGF96/zNL+R8fjPJKBqAO7SOANE/hMJ+FDPv7GEmPwnElBdQK0lROU/j4CGAmorIS7/aQTU3oAeUl1CZP7TCGgqoGMqS4jNfxYBjQV0TFUJ0flPIuBLO/+qEuLzn0RAxwGoOQIC/OcQ0LrAlUdAgf8cApoX+C47OyzBfwoBfQdg7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uQgWY/34iBbh/KhIowM9/TeIEmH9VwgS4f+oSJcDPf2WCBJh/bWIEuH+qEyLAz399IgSYf0MCBLh/WoIX4Oe/KXAB5t8WtAD3T2PAAvz8twYrwPybAxXg/mkPUoCf/44ABZh/T3AC3D9dgQnw898XlADz7wxIgPn35n/8oSIWFCbeewAAAABJRU5ErkJggg==" width="192">

The following code demonstrates that we can use a group multiple
times.  In this case, we use the group in two further viewports,
both of which are still square, but smaller than the image and 
shifted to the left or right.

Another difference in this example is that the group is based on 
a rectangle that is drawn within a viewport that has a luminance
mask applied;  further evidence of our ability to employ the
various graphical tools in combination with each other.

```{r eval=FALSE, transform-mask, fig.keep="none"}
pdf("luminance-mask-squashed.pdf", width=2, height=2)
vp <- viewport(mask=as.mask(mask, "luminance"))
grob <- rectGrob(gp=gpar(fill=4), vp=vp)
grid.define(grob, name="donut")
grid.segments(gp=gpar(col=3, lwd=50))
pushViewport(viewport(x=.25, width=.5, height=.5))
grid.use("donut")
popViewport()
pushViewport(viewport(x=.75, width=.5, height=.5))
grid.use("donut")
popViewport()
dev.off()
```
```{r eval=FALSE, echo=FALSE}
system("pdftoppm -png -rx 96 -ry 96 luminance-mask-squashed.pdf > luminance-mask-squashed.png")
```

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMAAAADACAIAAADdvvtQAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAK80lEQVR42u3db2gb5x0H8McnK4qsSLpKVic7InSJRKtQ+4UhmOKYgfWu4JJARgdmpNXrQVMSSl8ltDAoJSEZ7LW6MgxlCyQk0FezYbihGA8PnBKtyAkjqLY2J6rkqyzZ8t3thcB0+aM7Pc89d/ecvt+3yZ3j333y/Lk/zzOg6zpBPBRlf/vy9x/U93+058dJqDj0ABDijB4Agh4AQpzTA0DQA0DQ45weAIIeAIIeRwNA0ANA0ANAiIh6AAh6AAh6AAgRVA8AQQ8AQQ8AIYLqASDoASDoASBEUD0ABD0ABD0AhAiqB4CgB4CgB4CgR1A9AAQ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t8zXcr0IPQt0DQg1g2jYcepH8BQQ8AQQ8AQQ8AQQ/SL4CgB4CgB4CgB4CgB+kXQNADQNADQNCDiAUIegAIegAIehCxAEEPAEEPAEEPIhYg6AEg6AEg6EHEAgQ9AAQ9AAQ9iFiAoAeAoAeAoAcRCxD0ABD0ABD0IGIBgh4Agh7ECUDQA0DQg/xf/geiQPc0dWmeSQAAAABJRU5ErkJggg==" alt="demonstration of affine transformtion of luminance masked output">



The new features have only been implemented on a subset of graphics 
devices so far: `cairo_pdf()`, `cairo_ps()`,
`x11(type="cairo")`, `png(type="cairo")`,
`jpeg(type="cairo")`, `tiff(type="cairo")`, `svg()`, and `pdf()`.
Furthermore, most compositing operators only work on the Cairo devices
and luminance masks only work on the `pdf()` device.

R packages that implement graphics devices will need to be updated and 
reinstalled for the new R version.

Further discussion and more detail about the new features and
how they have been implemented can be found in a series of
technical reports:  one on
[groups](https://stattech.wordpress.fos.auckland.ac.nz/2021/11/15/2021-02-groups-compositing-operators-and-affine-transformations-in-r-graphics/),
one on 
[paths](https://stattech.wordpress.fos.auckland.ac.nz/2021/11/16/2021-03-stroking-and-filling-paths-in-r-graphics/),
and one on 
[masks](https://stattech.blogs.auckland.ac.nz/2021/12/01/2021-04-luminance-masks-in-r-graphics/).



