r - ggplot2在编织时不打印字体,但以块的形式显示
问题描述
我正在尝试使用 R Markdown 创建 pdf 报告。我将 YAML 标头中的 mainfont 和我的绘图函数更改为格鲁吉亚。报告中的文本很好,当我运行单个代码块时,我看到了我的绘图文本,但是当我编织报告时,打印的绘图没有任何文本。这是我所拥有的一个例子......
---
title: ''
output:
pdf_document:
latex_engine: xelatex
html_document: default
word_document: default
mainfont: Georgia
urlcolor: blue
classoption: landscape
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(autodep = TRUE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
knitr::opts_chunk$set(fig.dim = c(11,7))
options(digits=3)
library(dplyr)
library(ggplot2)
library(tidyr)
library(readr)
library(ggpubr)
library(gridExtra)
library(stringr)
library(tinytex)
library(knitr)
library(kableExtra)
library(cowplot)
library(reshape2)
library(extrafont)
font_import()
loadfonts(device = "win")
run_chunk <- FALSE
```
```{r data, include=FALSE}
### Create df from scratch here
measure <- c('msr1', 'msr2', 'msr3')
rate <- c(50, 70, 95)
df <- data.frame(measure, rate)
```
The Markdown text outside of the code chunks works fine.
```{r lollipop_plot, eval=run_chunk}
lolli <- ggplot(df, aes(x=measure, y=rate)) +
geom_segment(aes(x=measure, xend=measure, y=0, yend=rate), color='grey') +
geom_point(size=4, alpha=0.6) +
ylim(0, 100) +
theme_classic() +
theme(axis.text.x=element_text(size=12)) +
theme(axis.text.y=element_text(size=12)) +
xlab('') +
ylab('Measure Percentile') +
ggtitle('Lollipop Plot') +
theme(axis.title.x=element_text(size=14)) +
theme(text=element_text(family="Georgia")) +
coord_flip() +
geom_hline(yintercept=50) +
theme(legend.position='bottom')
print(lolli)
```
我知道还有其他类似的问题,但我还没有看到这个问题的解决方案。
解决方案
编辑
该软件包似乎比依赖非常旧的库的软件包showtext
执行得更好(并且与系统无关) 。extrafont
它在 CRAN 上,他们的自我解释小插图可以在这里找到。
原始答案
我在 Windows 10 上使用 TinyTeX 将我的 R Markdown 文件呈现为 PDF 文档时遇到了同样的问题(模板tex
文件使用 Calibri 字体):
如果我运行extrafont::font_import()
(如上面评论中所建议的那样),实际上会跳过所有字体:
Scanning ttf files in C:\WINDOWS\Fonts ...
Extracting .afm files from .ttf files...
C:\Windows\Fonts\AGENCYB.TTF : No FontName. Skipping.
C:\Windows\Fonts\AGENCYR.TTF : No FontName. Skipping.
C:\Windows\Fonts\ALGER.TTF : No FontName. Skipping.
...
所以 R 仍然不“知道” Calibri,因此缺少情节标签。然后我发现这实际上是由于Rttf2pt1
R 包中的问题,这里提供了解决方案:https ://stackoverflow.com/a/68642855/4575331 。当前版本Rttf2pt1
是 1.3.9 引入了这个问题,因此降级到 1.3.8 有助于:
remotes::install_version("Rttf2pt1", version = "1.3.8")
extrafont::font_import()
然后在重新启动 R 并再次编织后一切都很好。
推荐阅读
- android - 谷歌安卓地图应用程序在从数据库中获取纬度和经度时崩溃
- react-native - 如何在反应导航 v4 中从底部选项卡导航器调用警报
- c++ - 如何计算文本文件中单词中的单个字符数?
- azure - 为 Azure 存储添加生命周期管理 (tierToArchive)
- python - Python pygame 客户端/服务器运行缓慢
- c# - 保存文件后在 C# 中关闭 Excel
- java - 我们可以使用 Object 类的实例作为 HashMap 中的键吗
- javascript - 如何在javascript中从windows.parent.location.href中提取基本Url
- php - 如何在 Laravel 中保持/传递经过身份验证的用户会话到其他域
- java - StringBuffer 会改变 JSON 的顺序吗?