首页 > 解决方案 > 打印和导出 PDF 与调用图形设备保存 PDF 有什么区别?

问题描述

我在 MacOS High Sierra 10.13.5 平台 x86_64-apple-darwin17.6.0 上运行 R 3.5.1 和 RStudio 1.1.453。我想了解为什么我可以打印和导出绘图但不能使用图形设备来保存绘图。

我可以使用 R 的 extrafont 包将 Corbel 字体嵌入到 PDF 图形中。我可以使用 print() 保存这些图表,然后通过 Plot 窗口导出。使用这种方法,字体嵌入和显示完美。

但是,我希望 R 保存几十个这样的图,而不必手动 print() 并保存每一个。为此有多种方法。

一种是 R 的PDF Graphics Device。不幸的是,这个设备似乎不能很好地与空格和其他字符交互。这是产生 ggplot 的函数:

jobreport_grouped <- function(table, N)
{
  #Group
  table <- table %>%
    group_by(Education, Desired) %>%
    summarize(n())

  #Order
  table <- arrange(table, desc(`n()`))

  #Response label
  response_count <- sum(table$`n()`)

  #Sample size label
  samplesize <- paste("(N = ", sum(table$`n()`), ")", sep="")

  #Grouped barplot
  ggplot(table[which(table$`n()`>N),],
         aes(fill=Education, 
             x=reorder(Desired,-`n()`), 
             y=`n()`)
         ) +
    scale_fill_manual(values = paletteIDCN) +
    IDCNtheme_grouped_dense +
    xlab(paste("Number of Active Members: ", 
               member_count, 
               "\n", 
               "Number of Responses: ", 
               response_count, 
               sep="")
         ) +
    ylab("Number of IDCN Members") +
    ggtitle(paste(desired_title("Professional Areas"), 
                  "\n",
                  today, 
                  sep = ""
                  )
            )+
    geom_col(position="dodge")  
}

然后调用此函数并尝试编写情节

> getGroupedJobReport <- function(A,n){
    +   p <- jobreport_grouped(A,n)
    +   ggsave(p, filename = "~/TMP-R/TDP/plots/Desired_GROUPED.pdf", device = pdf,
    +          width = 12, height = 7, units = "in")
    + }
    > getGroupedJobReport(A1,2)
    There were 50 or more warnings (use warnings() to see the first 50)
    > warnings()
    Warning messages:
    1: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
      font width unknown for character 0x20
    2: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
      font width unknown for character 0x27
    3: In grid.Call(C_textBounds, as.graphicsAnnot(x$label),  ... :
      font width unknown for character 0x20

在此之后,我尝试了Cairo的 MacPorts 安装以及CRAN 的 Cairo包,但得到了不同的错误:

> getGroupedJobReport <- function(A,n){
+   p <- jobreport_grouped(A,n)
+   ggsave(p, filename = "~/TMP-R/TDP/plots/Desired_GROUPED.pdf", device = cairo_pdf,
+          width = 12, height = 7, units = "in")
+ }
> getGroupedJobReport(A1,2)
Warning message:
In dev(file = filename, width = dim[1], height = dim[2], ...) :
  failed to load cairo DLL

R 的原生 PDF Graphics Device 和 Cairo 有哪些替代品?如果有的话,我应该考虑使用哪些?

标签: r

解决方案


推荐阅读