0

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.

Westcroft_to_Apse
  • 1,503
  • 4
  • 20
  • 29

1 Answers1

2

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.

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(dplyr)
library(ggplot2)

theme_set(theme_bw(base_family = "serif"))
```

```{r}
mtcars %>%
ggplot(aes(x = wt, y = mpg)) +
geom_point()
```

According to this post the font "Times New Roman" is accessible as "serif". You can check this with the command windowsFonts():

$serif
[1] "TT Times New Roman"

$sans
[1] "TT Arial"

$mono
[1] "TT Courier New"
Martina
  • 139
  • 1
  • 10
  • 1
    Thank you. Unfortunately, this question is not specifically about Windows or TNR. So, first, there may a different default serif font. And second, the font that I want to use is not actually TNR (I just happened to use it in this example). So this doesn't really get me to where I need to be. However, this solution does work in that it avoids the error message, so I'm voting it up even though (for the reasons above) I'm not accepting it as correct. – Westcroft_to_Apse May 24 '22 at 22:08
  • Did you check in the post the answers of `Mike Wise` (customized fonts for your system with package extrafont: https://cran.r-project.org/web/packages/extrafont/extrafont.pdf) or `D A Wells`? Does this help? – Martina May 24 '22 at 22:19
  • I'd love to check the answers of Mike Wise -- especially if they explain why the above code will render to HTML but not to PDF. Could you let me know where I can find them? (N.B. Yes, I'm using extrafont.) – Westcroft_to_Apse May 24 '22 at 22:27