编辑:此页面提供代码: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)
但是,我想知道这些命令来自哪里。它们未包含在官方文档(https://ggplot2.tidyverse.org/reference/ggsave.html)中可能的设备列表中。并且,cairo_png不存在;相反,键入=" cairp-png"是必要的,例如:
ggsave("test_cairo.png", type = "cairo-png")
有谁知道为什么这个论点是一次性设备=""和另一个时间类型=""?
我尝试过像
这样的代码ggsave("model.eps", type = "cairo")
或
ggsave("model.eps", type = "cairo-ps")
或
ggsave("model.eps", device = "cairo-ps")
但似乎没有任何效果。通常,是否可以使用开罗图形设备使用ggsave创建.eps文件?如果是这样,怎么样?
答案 0 :(得分:1)
您可以使用保存到EPS
ggsave("model.eps", device = "eps")
答案 1 :(得分:1)
您需要查看以了解差异的代码位于ggplot命名空间中名为plot_dev
的未导出函数中。您可以通过查看ggsave代码获得此信息。分派到设备的行是:
dev <- plot_dev(device, filename, dpi = dpi)
# Look at that function
getAnywhere(plot_dev) # not exported, so need getAnywhere
plot_dev
的逻辑是首先检查是否将“设备”值作为函数名给出,如果是,则仅调用该函数。这就是您提供的前两个电话中发生的情况。如果不是函数,并且没有给出“设备”的字符值(这是您的第三个调用中的情况),则plot_dev将基于“文件名”提供的文件名扩展名从函数的命名列表中分派。将类型参数传递给png
函数,以使用png
的'cairo'版本,而不是默认值。
这是可能的设备及其默认参数的列表。可以为这些默认值提供备用值,而“点”可以用于指定其他设备参数。 (有关详情,请参见各自的帮助页面):
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"))
请注意,前两个参数的值为eps
。这是一个内部定义的函数:
eps <- function(filename, ...) {
grDevices::postscript(file = filename, ..., onefile = FALSE,
horizontal = FALSE, paper = "special")
答案 2 :(得分:1)
TL; DR
您需要调用特定的pdf和ps cairo设备,而可以使用其自身的type参数将标准png设备设置为产生cario输出。
说明
device
的{{1}}参数可以使用设备功能,与预定义列表之一匹配的字符串,或保留为ggsave
(在这种情况下,可以从文件扩展名)。在任何情况下,都会调用设备功能。请注意,在使用函数形式时,您可能需要设置一些NULL
为您使用的字符或自动检测形式的参数。
包含默认情况下使用的大多数设备的ggsave
软件包还具有grDevices
和cario_pdf
设备,您可以将其传递给cairo_ps
。
没有ggsave
设备。但是,cairo_png
设备具有一个png
参数,该参数采用以下向量中的选项(至少在Windows上):type
。 c("windows", "cairo", "cairo-png")
在调用时将ggsave
规范传递给type
设备。