将文件编织为PDF时,如何解决绘图中的编码问题?

时间:2020-06-02 20:04:36

标签: r pdf ggplot2 encoding r-markdown

我目前正在使用Rmarkdown处理PDF报告,但是在绘图中遇到编码问题。这是我的YAML标头和设置块的样子:

---
title: T\'{i}tulo
output: bookdown::pdf_document2
header-includes:
   - \renewcommand{\contentsname}{Conte\'{u}do} 
   - \usepackage[labelsep=space]{caption}
   - \counterwithin{table}{subsubsection}
   - \usepackage{longtable}
   - \usepackage{float}
   - \floatplacement{figure}{H}
   - \inputencoding{latin1}
toc: yes
toc_depth: 4 
---

\inputencoding{utf8}

\renewcommand{\tablename}{Tabela}

\renewcommand{\thetable}{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}\alph{table}}



\pagebreak
knitr::opts_chunk$set(echo = FALSE, fig.pos = 'H')
library(readr)
library(knitr)
library(ggplot2)
library(ggrepel)
library(dplyr)
library(tidyr)
library(kableExtra)

如果我尝试从YAML标头中删除或更改命令- \inputencoding{latin1}\inputencoding{utf8},该文件将无法编织。
使用这些命令,我​​可以编织文件,但是当我使用特殊字符(葡萄牙语)时,图中的所有文本都以错误的编码显示。该编码问题不会在文档中的其他任何地方发生,而只会在绘图中发生。

例如,标题“Gráfico”显示为“Gráfico”。

文件和全局选项编码均设置为“ UTF-8”。这是sessionInfo()的输出:

R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server >= 2012 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=Portuguese_Brazil.1252  LC_CTYPE=Portuguese_Brazil.1252    LC_MONETARY=Portuguese_Brazil.1252
[4] LC_NUMERIC=C                       LC_TIME=Portuguese_Brazil.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] kableExtra_1.0.1 tidyr_0.8.2      dplyr_0.8.5      ggrepel_0.8.2    ggplot2_3.3.0    knitr_1.22      
[7] readr_1.3.1     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6      highr_0.7         pillar_1.3.1      compiler_3.5.1    tools_3.5.1      
 [6] digest_0.6.18     viridisLite_0.3.0 evaluate_0.13     tibble_2.0.1      gtable_0.2.0     
[11] pkgconfig_2.0.2   rlang_0.4.5       rstudioapi_0.9.0  yaml_2.2.0        xfun_0.6         
[16] withr_2.1.2       httr_1.4.0        stringr_1.3.1     xml2_1.2.0        hms_0.4.2        
[21] webshot_0.5.1     grid_3.5.1        tidyselect_0.2.5  glue_1.3.0        R6_2.3.0         
[26] bookdown_0.19     rmarkdown_2.1     purrr_0.3.0       magrittr_1.5      scales_1.0.0     
[31] htmltools_0.3.6   assertthat_0.2.0  rvest_0.3.2       colorspace_1.4-0  labeling_0.3     
[36] stringi_1.4.3     munsell_0.5.0     crayon_1.3.4   

问题:如何解决绘图中的编码问题?

这是一个情节的简单示例,例如我遇到的问题:

ggplot(mtcars, aes(x = mpg, y = cyl)) + 
        geom_bar(stat = "identity") +
        labs(title = 'Gráfico',
             subtitle = "Subtítulo")

0 个答案:

没有答案