在 .rmd 中的 ggplot 中包含 unicode,以多种格式呈现
Include unicode in ggplot in .rmd, render in multiple formats
在 ggplot2
中,可以在文本元素(geoms、轴标签等)中包含 unicode 字符。但是,当 ggplot objects 从 .rmd
文件渲染到 html_vignette
、html_document
和 pdf_document
时,我正在努力让这些字符可靠地渲染。 =26=]
我的问题还没有完全重现,但至少在这个设置下:
"Ghost Orchid" 版本 (077589bc, 2021-09-20) for macOS
Mozilla/5.0 (Macintosh; Intel Mac OS X 12_2_0) AppleWebKit/537.36 (KHTML, like Gecko) [=42= .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
header 中的 mainfont
字段,得到了更多 informative-looking 警告:
例如
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
这(可能)是图形设备问题。您可以使用块选项设置不同的设备。如果你有 {ragg} 包,这通常会给我可靠的结果。
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
dev = "ragg_png")
library(ggplot2)
library(dplyr)
```
如果觉得图形太粗糙也可以在选项里加上dpi = 150
在 ggplot2
中,可以在文本元素(geoms、轴标签等)中包含 unicode 字符。但是,当 ggplot objects 从 .rmd
文件渲染到 html_vignette
、html_document
和 pdf_document
时,我正在努力让这些字符可靠地渲染。 =26=]
我的问题还没有完全重现,但至少在这个设置下: "Ghost Orchid" 版本 (077589bc, 2021-09-20) for macOS Mozilla/5.0 (Macintosh; Intel Mac OS X 12_2_0) AppleWebKit/537.36 (KHTML, like Gecko) [=42= .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
header 中的 mainfont
字段,得到了更多 informative-looking 警告:
例如
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
这(可能)是图形设备问题。您可以使用块选项设置不同的设备。如果你有 {ragg} 包,这通常会给我可靠的结果。
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,
dev = "ragg_png")
library(ggplot2)
library(dplyr)
```
如果觉得图形太粗糙也可以在选项里加上dpi = 150