Graphics Devices in R

If you normally use R via R Studio, please read The R Studio "Plots" panel Section.

When we run code that produces a data visualisation (or just draws any graphics), R automatically opens a graphics device to render the image. In an interactive R session, this will normally be a window on screen.

However, we can also deliberately open a graphics device and control the format of the graphics output. For example, the following code generates a PDF file containing a plot. NOTE that the dev.off() function (to "turn the device off") is important to finish creating the PDF file.

pdf("myplot.pdf")
plot(1)
dev.off()

There are a lot of different graphics devices available, including some provided by R packages. For example, the windows() function creates an on-screen graphics window (on Microsoft Windows), png() creates a PNG file, and svg() creates an SVG file. The 'ragg' package provides a range of graphics devices for bitmap formats like PNG.

Differences between graphics devices

One reason for selecting a specific graphics device is that different graphics formats have different advantages and disadvantages. For example, the svg() device is a nice option for HTML output (e.g., for graphics within an R Markdown file) because it should produce a nice smooth result at any size (it is a "vector" format). On the other hand, the png() device should produce smaller files (it is a "raster" format) and those files can be used in a wider range of contexts, e.g., within HTML documents generated from R Markdown and within Microsoft Word documents and within PDF documents generated from LaTeX.

Another issue is that different graphics devices can sometimes produce (slightly) different output. As a simple example, the default background colour for a png() device is white, but it is transparent for a pdf() device. An example of a more significant difference is that the postscript() device cannot draw semitransparent colours (but both the png() and pdf() devices can).

Another important difference between graphics devices is that they will use slightly different fonts by default and some devices only provide access to a limited range of fonts.

An important set of devices are those based on the Cairo graphics system. This includes the svg() device, and png(type = "cairo"), and cairo_pdf(). These devices have consistent defaults, are able to produce the full range of graphical output (e.g., semitransparent colours), and they provide easy access to all of the available fonts that are installed on a computer.

Another important set of devices are those provided by the 'ragg' pacakge. These also have consistent defaults, produce the full range of graphical output, and provide easy access to the available fonts (and additional font features). The 'ragg' package web site has lots more information, including how to set 'ragg' as the default graphics device in R Studio.

Graphics devices in R Markdown documents

When we write code in an R code chunk within an R Markdown document that produces graphical output, the "knitting" process automatically opens a graphics device to capture the graphical output. That device creates a file that is automatically embedded within the HTML document that is produced.

We can control the graphics device that gets opened during "knitting" by specifying options for the R code chunk. For example, the following code chunk will capture output using an svg() graphics device, which will create an SVG file (and embed that SVG file in the HTML output).

```{r dev="svg"}
plot(1)
```
  

The following code chunk shows how we can specify options for the graphics device (in this case to select a png(type = "cairo") graphics device).

```{r dev="png", dev.args=list(type="cairo")}
plot(1)
```
  

NOTE that these options will work when the R Markdown document is "knitted" to HTML, but they might NOT work within the R Studio editor just by "running" the individual chunk to preview it (e.g., by clicking the little green "play" button at the top-right of the chunk).

The R Studio "Plots" panel

The R Studio "Plots" panel is not a normal R graphics device. This means that the output shown in the "Plots" panel may sometimes be incorrect and/or distorted and/or very slow to draw.

It may sometimes be necessary to deliberately open a normal R graphics device if the "Plots" panel does not produce the correct output.


Creative Commons License
This work is licensed under a Creative Commons Attribution 4.0 International License.