Edit: This page provides the code: https://www.andrewheiss.com/blog/2017/09/27/working-with-r-cairo-graphics-custom-fonts-and-ggplot/
ggsave("test_cario.eps", device=cairo_ps)
ggsave("test_cario.pdf", device=cairo_pdf)
However, I am wondering where the commands come from. They are not included in the list of possible devices in the official documentation (https://ggplot2.tidyverse.org/reference/ggsave.html). And, cairo_png does not exist; instead, type="cairo-png" is necessary, e.g.:
ggsave("test_cairo.png", type = "cairo-png")
Does anyone know why the argument is one time device = ""
and another time type = ""
?
I have tried code like
ggsave("model.eps", type = "cairo")
or
ggsave("model.eps", type = "cairo-ps")
or
ggsave("model.eps", device = "cairo-ps")
but nothing seems to work. In general, is it possible to create .eps files with ggsave using the Cairo graphics device? If so, how?
There are many advantages to using Cairo to save R graphics ( see here, for example ). When saving PDFs, for instance, the cairo_pdf device correctly embeds custom fonts. Using the cairo_pdf graphics device is easy with ggplot-based graphics with ggsave ():
Instead of specifying a device, we specify type: ggsave (ugly_plot, filename = "ugly_plot.png", width = 4, height = 2.5, dpi = 300, type = "cairo") Placing that file in Word or PowerPoint works great and everything is sized correctly at high resolution.
There is no cairo_png device. However, the png device has a type parameter which takes options from the following vector (on Windows as least): c ("windows", "cairo", "cairo-png"). ggsave passes on the type specification to the png device when it calls it. Thanks!
Saving ggplot output as high resolution Cairo PNGs is easy with ggsave (), but the syntax is slightly different from saving as Cairo PDFs. Instead of specifying a device, we specify type: ggsave (ugly_plot, filename = "ugly_plot.png", width = 4, height = 2.5, dpi = 300, type = "cairo")
The code you need to look at to understand the differences is in a non-exported function named plot_dev
in the ggplot namespace. You get this information by looking at the ggsave code. The line that dispatches to a device is:
dev <- plot_dev(device, filename, dpi = dpi)
# Look at that function
getAnywhere(plot_dev) # not exported, so need getAnywhere
The logic of plot_dev
is to first check to see whether the "device" value was given as a function name and if so to just call that function. That is what happens in the first two calls you offered. If it's not a function and no character value for "device" is given (which is the situation in your third call), then plot_dev dispatchs from a named list of functions based on the extension of the file name offered as "filename". The type argument gets passed to the png
function to get the 'cairo' version of png
used rather than the default.
This is the list of possible devices and their default arguments. Those defaults can be offered alternate values and the "dots" can be used to specify other device parameters. (See their respective help pages for specifics):
devices <- list(eps = eps,
ps = eps,
tex = function(filename, ...)
grDevices::pictex(file = filename, ...),
pdf = function(filename, ..., version = "1.4")
grDevices::pdf(file = filename, ..., version = version),
svg = function(filename, ...) vglite::svglite(file = filename, ...),
emf = function(...) grDevices::win.metafile(...),
wmf = function(...) grDevices::win.metafile(...),
png = function(...) grDevices::png(..., res = dpi,
units = "in"),
jpg = function(...) grDevices::jpeg(..., res = dpi,
units = "in"),
jpeg = function(...) grDevices::jpeg(..., res = dpi,
units = "in"),
bmp = function(...) grDevices::bmp(..., res = dpi,
units = "in"),
tiff = function(...) grDevices::tiff(..., res = dpi,
units = "in"))
Notice that the first two parameters are given values of eps
. That is an internally defined function:
eps <- function(filename, ...) {
grDevices::postscript(file = filename, ..., onefile = FALSE,
horizontal = FALSE, paper = "special")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With