0

The plot below was generated in R (without any issue), using this code:

library(tidyverse)
library(extrafont)
loadfonts()

x <- rexp(100)

data.frame(info = x) %>% 
ggplot() + 
geom_histogram(aes(x = info), col = "red", fill = "red", alpha = 0.5) +
theme_minimal() +
theme(text = element_text(family="LM Roman 10"))

Image in Chunk

As you can see, the font of the plot is set to "LM Roman 10", which I was able to do thanks to this post and it works perfectly within R.

However when I try to place the image in a LaTeX document using RMarkdown, I get this error:

Quitting from lines 10-22 (min_example.Rmd) 
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
Además: There were 50 or more warnings (use warnings() to see the first 50)
Ejecución interrumpida
    

Here is the code for min_example.Rmd

---
title: "Untitled"
author: "Javier Rojas"
date: "2/9/2020"
output: pdf_document
---


```{r, echo=FALSE}

library(tidyverse)
library(extrafont)
loadfonts()

x <- rexp(100)

data.frame(info = x) %>% 
ggplot() + 
geom_histogram(aes(x = info), col = "red", fill = "red", alpha = 0.5) +
theme_minimal() +
theme(text = element_text(family="LM Roman 10"))
```

I am using a Mac computer running macOS High Sierra and R 3.6.1

Javier
  • 332
  • 1
  • 4
  • 14
  • Have you installed the Latin modern fonts? see [this](https://stackoverflow.com/questions/24458870/how-can-i-make-an-r-plot-use-the-latin-modern-font-family-when-saved-as-a-pdf), there is an answer with the instructions. – Pedro Fonseca Sep 03 '20 at 03:18
  • Yes Pedro, I have. The code runs without problems in R, the issue happens when I place that code inside an RMarkdown Chunk – Javier Sep 03 '20 at 05:20

2 Answers2

1

Usually quite easy to solve. The problem should be, that the font is not installed in your computer.

You have to download the .otf file for the font e.g. (https://fonts2u.com/lmroman10-regular.font) and install it on your Operating System.

If you don't know how to do this, just google it (e.g. "install extra font Windows"), there are plenty of tutorials on it online.

-edit- I was a little to quick - didn't realize the problem just comes from running it in rmarkdown. Try the following:

```{r, fig.showtext=TRUE, echo=FALSE}
library("tidyverse")
library("showtext")

x <- rexp(100)

font_add("LM Roman 10", regular = "lmroman10-regular.otf")

data.frame(info = x) %>% 
ggplot() + 
geom_histogram(aes(x = info), col = "red", fill = "red", alpha = 0.5) +
theme_minimal() +
theme(text = element_text(family="LM Roman 10"))
      
```

It's important that you add fig.showtext=TRUE, library("showtext") and font_add("LM Roman 10", regular = "lmroman10-regular.otf").

I just placed the .otf in my project folder - but I think you can also give it another path.

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
  • Hi Steffen! No, the font is installed. I can even use it in Microsoft Word. As I mentioned, the code runs without problems in R, the issue happens when I place that code inside an RMarkdown Chunk – Javier Sep 03 '20 at 20:47
  • Oh, are you really sure about this ? "invalid font type" is usually exactly the message you get, when you have not installed the font. I get nearly the same error message, when I have a font not installed. While there is no error (also for your example) with the font installed. I also have macOS. I would check, if you really have the right font installed (try the .odt from my link). Also try to install it at the Mac Font collection under User. Also make sure you reference it in R under the right name (as added to the font collection) – Steffen Moritz Sep 03 '20 at 21:33
  • Yes, I just installed the .odt from your link. I still get the same error. I checked and the font is installed under user. How would you reference the font properly in R? loadfonts("Latin Modern Roman")? – Javier Sep 03 '20 at 23:39
  • Yes....but I am sorry, I read it too quick xD Didn't execute it without rmarkdown ;) Which as you already said works fine - wait I'll also post how to do it in rmarkdown – Steffen Moritz Sep 04 '20 at 00:22
1

There exists a current new approach using showtext and showtextdb packages.

  1. On Windows, install manually Latin Modern Roman font (.tff version) following these steps easy instructions https://tex.stackexchange.com/questions/55787/latin-modern-roman-for-ttf. After the installation, you can locate all your fonts in "C:/Windows/Fonts/" just in case.

  2. Once it's already installed, try the following R code to see and example:

install.packages("showtext")
install.packages("showtextdb")
library(showtext)
library(showtextdb)
#set the name and file path
font_add(family = "lmroman10", regular = "C:/Windows/Fonts/lmroman10-regular-webfont.ttf")
showtext_auto()
library(ggplot2)
p = ggplot(NULL, aes(x = 1, y = 1)) + ylim(0.8, 1.2) +
  theme(axis.title = element_blank(), axis.ticks = element_blank(),
        axis.text = element_blank()) +
  annotate("text", 1, 1.1, family = "lmroman10", size = 15,
           label = "Text using new font")
  • Thanks @OmarBenites for this answer. When I use showtext and font_add_google(...), the axis labels with math symbols, e.g. xlab=expression("Coefficient,"~beta[1]), fail to render the math symbols properly upon knitting my RMarkdown PDF. Do you know how to fix this? – jdcode Nov 25 '22 at 20:22