Zakary Krumlinde
Zakary Krumlinde

Reputation: 139

ggplot2 not printing font when knit, but shows up in chunks

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.

Upvotes: 1

Views: 810

Answers (1)

MS Berends
MS Berends

Reputation: 5269

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.

Upvotes: 1

Related Questions