Search code examples
rggplot2r-markdownmarkdown

How to show foreign characters in ggplot output in RMarkdown?


I am currently working on a paper with ggplot plots which includes Turkish characters in labels. When I knit this RMarkdown file, Turkish characters such as "ğ" and "ı" are seen as "." in the plots.

ggplot(cars,aes(x = speed, y = dist))+
  geom_point()+
   labs(x = "Hız")

when I knit this chunk there is a warning ## Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :## conversion failure on ’Hız’ in ’mbcsToSbcs’: dot substituted for <b1> and the output is output of the ggplot.

I tried to put the HTML codes of the characters in to labels but it also didn't work. plot with html code

How can I fix this issue?


Solution

  • The simpliest way using ggsave and \includegraphics. (PDF output)

    ---
    title: "rmd2023"
    output:
      pdf_document: 
        latex_engine: xelatex      
    date: '2023-04-11'
    ---       
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(tidyverse)
    ```
    
    ```{r cars, include=F, warning=F}
    ggplot(cars,aes(x = speed, y = dist))+
      geom_point() +
       labs(x = "H\u0131z") 
    
    ggsave("Turk.png", dpi = 300)
    ```
    
    \includegraphics{Turk.png}
    

    enter image description here