【问题标题】:Include unicode in ggplot in .rmd, render in multiple formats在 .rmd 中的 ggplot 中包含 unicode,以多种格式呈现
【发布时间】:2022-02-03 17:38:27
【问题描述】:

ggplot2 中,可以在文本元素(几何、轴标签等)中包含 unicode 字符。但是,当 ggplot 对象从 .rmd 文件渲染到 html_vignettehtml_documentpdf_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


【解决方案1】:

这(可能)是图形设备问题。您可以使用块选项设置不同的设备。如果你有 {ragg} 包,这通常会给我可靠的结果。

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

如果发现图形太粗糙,也可以在选项中添加dpi = 150

【讨论】:

  • 谢谢,这看起来很有用!关注:我想如果在包小插图中包含这一行将意味着向包添加依赖项?
  • 不是硬依赖。您可以将它放在“建议”中,以便持续集成/CRAN 知道要下载什么,但用户不必安装它。我们在 {geomtextpath} 中也这样做了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 1970-01-01
  • 2011-02-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多