繁体   English   中英

ggplot 中的 Rscript 无效字体类型

[英]Rscript invalid font type in ggplot

我有一个使用非标准字体生成 ggplot 的简短脚本(请参阅下面的 MWE)。

如果我在 R(即 RStudio)中运行脚本,一切都按预期工作,我没有看到任何错误。

当我使用命令行和Rscript运行脚本时,出现错误:

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  invalid font type
Calls: <Anonymous> ... drawDetails -> drawDetails.text -> grid.Call.graphics
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Execution halted

我用Rscript myscript.R执行脚本,脚本的内容是

library(ggplot2)
theme_set(theme_light(base_family = "LM Roman 10"))
# if "base_family" is left empty, everything works with Rscript

p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point()

ggsave("myplot.pdf", p, device = cairo_pdf)

我使用 Ubuntu 18.04.1 和 R 3.5.1。 我还更新了我所有的包裹。

当我使用R CMD BATCH myscript.R时,一切正常。

临时解决方案

如果您有同样的问题(到目前为止我还无法解决),我已经恢复使用R CMD BATCH --vanilla myscript.R

我发现,如果您使用 extrafont 库将所有系统字体导入 R,这往往会解决问题。

执行此操作的简单方法是在终端中运行 R 并执行类似的操作

$ R

R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

> library(extrafont)
Registering fonts with R
> extrafont::font_import()
Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
Continue? [y/n] y

按回车键后,应导入所有系统字体。

另一种可能是您需要为 Ghostscript 设置一个环境变量,这可以通过在您的库调用后添加以下内容来完成

Sys.setenv(R_GSCMD="/usr/bin/ghostscript")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM