knitr::opts_chunk$set(
  comment = "#>",
  collapse = TRUE
)

Most systems should be able to use Cairographics (or cairo) based graphics devices. To test for this functionality, run

capabilities("cairo")

If you are using an Apple computer, then you have access to quartz, which starts the device driver for the OS X system^[Thanks to Bob Rudis for running the Apple example.].

capabilities("quartz")

When saving a file as a png, we can specify which graphics device to use using the type argument, e.g.

png("figure1.png", type="cairo")

The three main operating systems differ in their default graphics device, i.e the value they use if the type argument is ommitted from the png function call. For Linux and Apple systems, the default is typically optimal. However for Windows, it is worthwhile changing to cario

dd = data.frame(OS = c("Windows", "Apple", "Linux"), 
                Default = c("windows", "quartz", "cairo"), 
                Recommendation = c("cairo", "quartz", "cairo"))
knitr::kable(dd, caption="Default values of the `type` argument in the `png` function.")

Windows

knitr::include_graphics("graphics/windows_default.png", auto_pdf = FALSE)
knitr::include_graphics("graphics/windows_cairo.png", auto_pdf = FALSE)
knitr::include_graphics("graphics/windows_cairo-png.png", auto_pdf = FALSE)
knitr::include_graphics("graphics/windows_windows.png", auto_pdf = FALSE)

Apple

knitr::include_graphics("graphics/apple_default.png", auto_pdf = FALSE)
knitr::include_graphics("graphics/apple_cairo.png", auto_pdf = FALSE)
knitr::include_graphics("graphics/apple_cairo-png.png", auto_pdf = FALSE)
knitr::include_graphics("graphics/apple_quartz.png", auto_pdf = FALSE)

Linux

knitr::include_graphics("graphics/linux_default.png", auto_pdf = FALSE)
knitr::include_graphics("graphics/linux_cairo.png", auto_pdf = FALSE)
knitr::include_graphics("graphics/linux_cairo-png.png", auto_pdf = FALSE)
knitr::include_graphics("graphics/linux_Xlib.png", auto_pdf = FALSE)

Appendix

## Test function
f = function(title="") {
  set.seed(1)
  par(mar=c(3,3,2,1), mgp=c(2,0.4,0), tck=-.01,
      cex.axis=0.9, las=1)
  plot(rnorm(100), rnorm(100), pch=21, bg="steelblue", 
       main=paste(title, "device"), 
       xlab="x-axis label", ylab="y-axis label", 
       xlim=c(-3,3), ylim=c(-3,3))
  text(0,3,
       "Le français, c'est façile: Règles, Liberté, Egalité, Fraternité...", 
       cex=0.8)
  text(-2,-3, "italic text", font=3, cex=0.9)
  text(2,-3, "Bold text", font=2, cex=0.9)
  grid()
}


csgillespie/efficient documentation built on Jan. 26, 2020, 4:03 a.m.