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