11.15 High-quality graphics (*)
The rmarkdown package has set reasonable default graphical devices for different output formats. For example, HTML output formats use the
png() device, so knitr will generate PNG plot files, and PDF output formats use the
pdf() device, etc. If you are not satisfied by the quality of the default graphical devices, you can change them via the chunk option
dev. All possible devices supported by knitr are:
Usually a graphical device name is also a function name. If you want to know more about a device, you can read the R help page. For example, you can type
?svg in the R console to know the details about the
svg device, which is included in base R. Note that the
quartz_XXX devices are based on the
quartz() function, and they are only available on macOS. The
CairoXXX devices are from the add-on R package Cairo (Urbanek and Horner 2020), the
Cairo_XXX devices are from the cairoDevice package (Lawrence 2019), the
svglite device is from the svglite package (Wickham, Henry, et al. 2020), and
tikz is a device in the tikzDevice package (Sharpsteen and Bracken 2020). If you want to use devices from an add-on package, you have to install the package first.
Usually vector graphics have higher quality than raster graphics, and you can scale vector graphics without loss of quality. For HTML output, you may consider using
dev = "svg" or
dev = "svglite" for SVG plots. Note that SVG is a vector graphics format, and the default
png device produces a raster graphics format.
For PDF output, if you are really picky about the typeface in your plots, you may use
dev = "tikz", because it offers native support for LaTeX, which means all elements in a plot, including text and symbols, are rendered in high quality through LaTeX. Figure 11.2 shows an example of writing LaTeX math expressions in an R plot rendered with the chunk option
dev = "tikz".
Note that base R actually supports math expressions, but they are not rendered via LaTeX (see
?plotmath for details). There are several advanced options to tune the typesetting details of the
tikz device. You may see
?tikzDevice::tikz for the possibilities. For example, if your plot contains multibyte characters, you may want to set the option:
That is because
xetex is usually better than the default engine
pdftex in processing multibyte characters in LaTeX documents.
There are two major disadvantages of the
tikz device. First, it requires a LaTeX installation, but this may not be too bad (see Section 1.2). You also need a few LaTeX packages, which can be easily installed if you are using TinyTeX:
Second, it is often significantly slower to render the plots, because this device generates a LaTeX file and has to compile it to PDF. If you feel the code chunk is time-consuming, you may enable caching by the chunk option
cache = TRUE (see Section 11.4).
For Figure 11.2, we also used the chunk option
fig.process = pdf2png, where the function
pdf2png is defined in Section 11.14 to convert the PDF plot to PNG when the output format is not LaTeX. Without the conversion, you may not be able to view the PDF plot in the online version of this book in the web browser.
Lawrence, Michael. 2019. CairoDevice: Embeddable Cairo Graphics Device Driver. https://CRAN.R-project.org/package=cairoDevice.
Sharpsteen, Charlie, and Cameron Bracken. 2020. TikzDevice: R Graphics Output in Latex Format. https://github.com/daqana/tikzDevice.
Urbanek, Simon, and Jeffrey Horner. 2020. Cairo: R Graphics Device Using Cairo Graphics Library for Creating High-Quality Bitmap (Png, Jpeg, Tiff), Vector (Pdf, Svg, Postscript) and Display (X11 and Win32) Output. http://www.rforge.net/Cairo/.
Wickham, Hadley, Lionel Henry, Thomas Lin Pedersen, T Jake Luciani, Matthieu Decorde, and Vaudor Lise. 2020. Svglite: An Svg Graphics Device. https://github.com/r-lib/svglite.