更改R Markdown中的GGPLOT2字体:编织到PDF时失败(但在编织到HTML时不会)
我有一个通过knitr,pandoc和latex将其编译到PDF的R Markdown文件。它包含使用GGPLOT2创建的几个数字。我想更改图中使用的字体以匹配用于主体文本的字体。
编织到HTML时,以下代码正常,即图形出现在轴上新罗马时出现。但是,在编织到PDF时会引发错误。
---
title: "Font test"
author: "Westcroft to Apse"
date: '2022-05-24'
output:
pdf_document: default
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(ggplot2)
theme_set(theme_bw(base_family = "Times New Roman"))
```
```{r}
mtcars %>%
ggplot(aes(x = wt, y = mpg)) +
geom_point()
```
这是我尝试编织PDF时出现在“渲染”选项卡中的错误消息:
Line 21 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
如何解决此问题?
编辑:要明确,这不是关于使用时代新罗马的问题。实际上,这不是我要使用的字体。我只是在这里以此为例,说明了相同代码将如何呈现为HTML而不是对PDF。
I have an R Markdown file which I am compiling to PDF via knitr, Pandoc, and LaTeX. It contains several figures created using ggplot2. I want to change the font used in the figures to match the font used for the body text.
The following code works fine when knitting to HTML, i.e. the figure appears with Times New Roman on the axes. However, when knitting to PDF, it throws an error.
---
title: "Font test"
author: "Westcroft to Apse"
date: '2022-05-24'
output:
pdf_document: default
html_document:
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(dplyr)
library(ggplot2)
theme_set(theme_bw(base_family = "Times New Roman"))
```
```{r}
mtcars %>%
ggplot(aes(x = wt, y = mpg)) +
geom_point()
```
This is the error message which appears in the Render tab when I attempt to knit to PDF:
Line 21 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
How can I fix this?
EDIT: To be clear, this is not a question about using Times New Roman. That is not, in fact, the font I want to use. I just used it here as an example of how the same code will render to HTML but not to PDF.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在使用Windows吗?如果是这样,问题在于R在您的参考文献中无法识别字体“ Times New Roman”。将名称更改为“ Serif”,以将新罗马作为情节的字体设置。
根据此 post font“ font“ times new times new Roman”可访问为“ Serif”。您可以使用命令
Windowsfonts()
:Are you using Windows? If so, the problem is that R doesn't recognize the font "Times New Roman" with your reference. Change the name to "serif" to set Times New Roman as the font for the plot.
According to this post the font "Times New Roman" is accessible as "serif". You can check this with the command
windowsFonts()
: