r - 使用 ggplot2 对象和自定义字体从终端运行 R 脚本(字体类型无效)
问题描述
我有一个在 R 和 RStudio 中运行良好的脚本,但是当我Rscript mycode.R
在终端(macOS)中运行时,它返回以下错误:
Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, :
invalid font type
Calls: stocks_report ... drawDetails -> drawDetails.text -> grid.Call.graphics
Execution halted
我知道它正在发生,因为我使用了lares::theme_lares2()
将绘图字体设置为“Arial Narrow”的函数。但是,为什么通过终端运行会出现问题?
可重复的小例子。这段代码运行得很好。您可以将其保存到mycode.R
文件中。
library(ggplot2)
library(lares) # devtools::install_github("laresbernardo/lares")
data(diamonds)
ggplot(diamonds, aes(cut, price)) + geom_boxplot() + theme_lares2()
尝试运行Rscript mycode.R
,将返回相同的错误。
PS:请耐心安装lares
库......它有很多依赖项。谢谢!:)
解决方案
如果这是您的代码,那么这可能是因为您没有告诉 R 将您的绘图输出到文件中。这很好,因为它会在 Rstudio 中自动绘制它,但在命令行中没有图形。我会尝试在此处添加一些保存图形的方式
pdf("where_file.pdf")
ggplot(diamonds, aes(cut, price)) + geom_boxplot() + theme_lares2()
dev.off()
推荐阅读
- javascript - 使用循环向 Firestore 的 get() 添加多个条件,一般 javascript 问题
- python - 使用 PyQt5 实时绘图
- java - 为什么我可以在没有 getter 方法的情况下访问私有实例变量?
- nginx - Nginx 将 url 参数作为标头传递
- c - CS50破解问题。需要有关如何解密 2-4 个字符单词的帮助
- asp.net - 允许匿名访问特定的 aspx 文件不起作用
- django - Angular8:调用 localhost Rest Api 服务
- nginx - 将服务器中的文件列表作为 json 或文本返回,而不是默认的“索引”html 页面
- html - How can I vertically center a div on any resolution?
- azure - 在 Azure 的 Tomcat Web App 中定位标准输出日志