【发布时间】:2022-02-03 17:38:27
【问题描述】:
在ggplot2 中,可以在文本元素(几何、轴标签等)中包含 unicode 字符。但是,当 ggplot 对象从 .rmd 文件渲染到 html_vignette、html_document 和 pdf_document 时,我很难让这些字符可靠地渲染。
我的问题还没有完全重现,但至少在这个设置下: 适用于 macOS 的“幽灵兰花”发布(077589bc,2021-09-20) 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",
使用Rstudio 中的Knit 按钮输出到.pdf 时,我无法正确呈现unicode
---
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()```
这个问题已经在here 和其他地方进行了讨论,但解决方案似乎非常特定于仅使用cairo 设备保存.pdf 文件。什么是包含 unicode 字符的更通用方法,以便它们在不同的输出格式(包括 pdf_document)中正确呈现。
编辑:
我摆弄了YAML 标头中的mainfont 字段,得到了一堆看起来更丰富的警告:
例如
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
【问题讨论】:
标签: r ggplot2 unicode r-markdown knitr