Failed to compile a pdf file having ggplot with Chinese elements from Rmarkdown

I follow the instructions to install TinyTex and create a pdf document supporting non-English language. But it didn't work.

Below is my Rmarkdown code, I write `` here just to avoid auto creation of a new code block.

---

title: "测试文件"
documentclass: ctexart
output:
  rticles::ctex: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(tinytex.verbose = TRUE)
``

## 我在测试
正文可以用中文,但ggplot不行,悲伤
```{r}
library(ggplot2)
ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose, group=dose)) +
  geom_boxplot()+
  labs(title='标题',
       caption='来源: 嘻嘻')
``

And Error message here:

Thanks!

I think one problem is that you have the chunk defined as

library(ggplot2)
ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose, group=dose)) +
  geom_boxplot()+
  labs(title='标题',
       caption='来源: 嘻嘻')
``

and you need
``` at the bottom.

Okay it looks like you need to specify an output latex engine such as latex_engine: xelatex. The default pdflatex does not seem able to d\handle Chinese characters.

Also there is a problem using Chinese characters in the ggplot command. I do not know enough about R encoding to find the problem

Here is some code that almost works but it breaks if I use Chinese characters for the ggtitle. With ggtitle("HI") it compiles and with ggtitle("标题") it breaks

title: "测试文件"
author: "jrkrideau"
date: "20/10/2020"
documentclass: ctexart
output:
  rticles::ctex:
    latex_engine: xelatex
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(texlive.verbose = TRUE)

This is just simple attempt. 测试文件

我在测试

正文可以用中文,但ggplot不行,悲伤

library(ggplot2)
dat1  <-  data.frame(xx = sample(1:2, 100, replace = TRUE), yy = sample(1:10, 100, replace = TRUE))

ggplot(dat1, aes( yy)) + geom_histogram(bins = 7 ) + ggtitle("HI")


I am sorry I can not be of more help.

Thanks for the response. Everything solve after I update my TexLive to 2020 version.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.