French Apostrophe is turned into ... when exporting ggplot to PDF

Viewed 91

When saving a ggplot as pdf the french apostrophe is turned into ...

While, saving as png behaves as expected preserving the apostrophe.

See images for examples of the output.

Correct export

Incorrect export

I am exporting an rmd file to pdf through latex, and the ggplot figures are saved as pdf.

How can I export to pdf and preserve the apostrophe in the titles?

1 Answers

I can't be certain without seeing your code, but switching to the cairo_pdf output device will probably resolve this.

The rmarkdown example file below shows PDF output with the pdf device (which is the default) and the cairo_pdf device. I've used the curly single apostrophe (unicode character \u2019), which I think is the character you're trying to render.

In the rmarkdown file, I've set a separate output device in each code chunk. However, you can set the output device once for the entire document by running the following in the first code chunk:

knitr::opts_chunk$set(dev="cairo_pdf")

Example rmarkdown document with PDF output

---
output: pdf_document
---

```{r include=FALSE}
knitr::opts_chunk$set(warning=FALSE, message=FALSE)
library(tidyverse)
```

```{r, fig.height=2, dev="pdf"}

ggplot(mtcars, aes(mpg, hp)) + geom_point() +
  labs(title = "C\u2019est le titre") +
  theme_bw(base_size=15)
```

```{r, fig.height=2, dev="cairo_pdf"}

ggplot(mtcars, aes(mpg, hp)) + geom_point() +
  labs(title = "C\u2019est le titre") +
  theme_bw(base_size=15)
```

enter image description here

Related