JerryWho
JerryWho

Reputation: 3105

How to use otf-font in ggplot2 graphics?

I want to use an otf-font in my ggplot2 graphics. I'm using Mac OS X 10.9. I found http://goo.gl/dFqJhV

But the experimental part won't work for me.

http://goo.gl/rNmIRR doesn't work either because I got lot's of errors compiling the branch. The mentioned branch seems to be too old.

The solution with cairo_pdf() (http://goo.gl/LcYFS6) does work but not for me, because I want to embed the graphics into a knitr file with output pdf and/or html.

Upvotes: 1

Views: 795

Answers (1)

yixuan
yixuan

Reputation: 834

You can try the showtext package combined with knitr using the fig.showtext=TRUE option.

Assume that the OTF font has a filename "somefont.otf", and you want to use it in R through the family name "myfont" (can be an arbitrary character string). Below is an example of an Rmd file that can be compiled into PDF or HTML:

---
output: pdf_document
---

```{r setup, include=FALSE}
library(showtext)
font.add("myfont", "somefont.otf")
```

```{r fig.showtext=TRUE, fig.width=7, fig.height=7}
plot(cars, family = "myfont")
```

Upvotes: 4

Related Questions