Search code examples
rggplot2cairoeps

Is it possible to create .eps files with ggsave using the Cairo graphics device?


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?


Solution

  • TL;DR

    You need to call specific pdf and ps cairo devices while the standard png device can be set to produce cario output using its own type parameter.

    Explanation

    The device parameter of ggsave can take a device function, a character string matching one of a predefined list, or be left as NULL (in which case the device is guessed from the file extension). In any case, a device function will be called. Note that when using the function form, you may need to set some parameters that ggsave would do for you if you used the character or auto-detection forms.

    The grDevices package which contains most of the devices used by default also has cario_pdf and cairo_ps devices that you can pass to ggsave.

    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.