1

I am trying to create pdf reports using R Markdown. I changed the mainfont in the YAML header and in my function for the plots to Georgia. The text in the report is fine, and when I run the individual code chunks, I see my plot text, but when I knit the report, the plots print without any text. Here is an example of what I have...

---
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)
```

I know there are other similar questions, but I have not seen a solution for this issue.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Where is `df` defined? If not in this document, any rendering will be subject to conditions in the parent environment (typically the console). Consider (1) making this reproducible *for us* by including a representative sample of `df` (please use `dput`), so that we can try this on our console; and (2) if you are not already doing this, when you render to pdf, use `rmarkdown::render(..., envir=new.env())` so that you don't have object-leakage. (If you *need* `df` from the console, then consider https://bookdown.org/yihui/rmarkdown/parameterized-reports.html) – r2evans Aug 25 '20 at 16:49
  • I updated to a reproducible code. – Zakary Krumlinde Aug 25 '20 at 18:28
  • Can you get the ggplot code to work on the console? I *can*, but when I try with the rmarkdown doc, I get over 1000 warnings about `Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)) : font metrics unknown for character 0x4d`. – r2evans Aug 25 '20 at 18:58
  • When I run the code chunks individually, it works great, Georgia font included, but then when I try to knit it, there is no text on the plot at all, the plot itself is there. I have gotten that warning as well. When I run 'font_import()', it shows Georgia as being there. (Georgia already registered with windowsFonts()). – Zakary Krumlinde Aug 25 '20 at 19:03
  • I can reproduce this problem. BTW, you definitely do not need to call `font_import()` in the report: when you install `extrafont`, it needs to be run *once* (for each installation), same for `loadfonts()` (in general) (ref: [README](https://github.com/wch/extrafont/)). However, even if I add `embed_fonts(...)` after rendering, I get the letters but they are all stacked/compressed, so ... not the answer. – r2evans Aug 25 '20 at 19:14

1 Answers1

1

EDIT

The showtext package seems to perform a lot better (and system-independent) than the extrafont package which relies on very old libraries. It is on CRAN here and their self-explaining vignette can be found here.

Original answer

I had the same problem, while using TinyTeX on Windows 10 to render my R Markdown file as PDF document (the template tex file uses the Calibri font):

plot without labels

If I run extrafont::font_import() (as suggested in above comments), all fonts are actually skipped:

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.
...

So R still does not "know" about Calibri, hence the missing plot labels. Then I found out that it was actually due to a problem in the Rttf2pt1 R package for which a solution was provided here: https://stackoverflow.com/a/68642855/4575331. The current version of Rttf2pt1 is 1.3.9 which introduced this problem, so downgrading to 1.3.8 helps:

remotes::install_version("Rttf2pt1", version = "1.3.8")
extrafont::font_import()

Then everything was fine after restarting R and knitting again.

MS Berends
  • 4,489
  • 1
  • 40
  • 53