0

In ggplot2, one can include unicode characters in text elements (geoms, axis labels, etc). However, I'm struggling to get these characters to render reliably when ggplot objects are rendered from .rmd files into html_vignette, html_document, and pdf_document.

I haven't gotten my issues to be fully reproducible, but at least with this setup: "Ghost Orchid" Release (077589bc, 2021-09-20) for macOS Mozilla/5.0 (Macintosh; Intel Mac OS X 12_2_0) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.10 Chrome/69.0.3497.128 Safari/537.36",

I'm not able to render the unicode properly when outputting to .pdf using the Knit button in Rstudio

---
title: "unicode_in_ggplot"
output: 
  rmarkdown::html_vignette: 
    fig_width: 3
    fig_height: 3
  rmarkdown::html_document:
    fig_width: 3
    fig_height: 3
    fig.retina: NULL
  rmarkdown::pdf_document: 
    fig_width: 3
    fig_height: 3
    latex_engine: xelatex
  word_document: default
vignette: >
  %\VignetteEncoding{UTF-8}
---

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


In the `.rmd` file I'd like to be able to render to
`pdf_document`. 

```{r unicode failure}
mtcars %>% ggplot(aes(cyl, mpg))+
  geom_point()+
  labs(title= "I can print kappa: \u03ba and ell: \u2113")+
  theme_classic()```

This issue has been discussed here and elsewhere, but the solutions seem pretty specific to just saving .pdf files with a cairo device. What is a more general way to include unicode characters so they render properly across different output formats, including pdf_document.

EDIT:

I fiddled with the mainfont field in the YAML header, and got a bunch of more informative-looking warnings:

e.g.

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :## conversion failure on 'I can print kappa:  and ell: ' in 'mbcsToSbcs': dot## substituted for <ce>## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :## conversion failure on 'I can print kappa:  and ell: ' in 'mbcsToSbcs': dot## substituted for <ba>## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :## conversion failure on 'I can print kappa:  and ell: ' in 'mbcsToSbcs': dot## substituted for <e2>## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :## conversion failure on 'I can print kappa:  and ell: ' in 'mbcsToSbcs': dot## substituted for <84>## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :## conversion failure on 'I can print kappa:  and ell: ' in 'mbcsToSbcs': dot## substituted for <93>## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :## conversion failure on 'I can print kappa:  and ell: ' in 'mbcsToSbcs': dot## substituted for <ce>## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :## conversion failure on 'I can print kappa:  and ell: ' in 'mbcsToSbcs': dot## substituted for <ba>## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :## conversion failure on 'I can print kappa:  and ell: ' in 'mbcsToSbcs': dot## substituted for <e2>## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :## conversion failure on 'I can print kappa:  and ell: ' in 'mbcsToSbcs': dot## substituted for <84>1
Michael Roswell
  • 1,300
  • 12
  • 31

1 Answers1

3

It's (likely) a graphics device issue. You can set a different device using chunk options. If you have the {ragg} package, this usually gives me reliable results.

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
                      dev = "ragg_png")
library(ggplot2)
library(dplyr)
```

You can also add dpi = 150 to the options if you find that the graphics are too coarse.

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Thanks, this looks useful! Concern: I imagine including this line would mean adding a dependency to the package if in a package vignette? – Michael Roswell Feb 03 '22 at 18:37
  • Not a hard dependency. You can put it in 'Suggests' so that continuous integration/CRAN knows what to download but users don't have to install it. We've done this in {geomtextpath} as well. – teunbrand Feb 03 '22 at 18:45