Home > database >  How do I fix the Chinese characters for my saved plot in R?
How do I fix the Chinese characters for my saved plot in R?

Time:11-16

Data

I tried looking through the threads here for an answer, but none of them were specific to my issue as far as I could tell. Here is some simulated data for my query.

#### Load Library ####
library(tidyverse)

#### Set Random Seed ####
set.seed(123)

#### Create Data ####
character <- c("哎","不","嗎","都","發",
               "你","肉","吧","地","有")
x <- round(rnorm(n=10,
                 mean=10))
y <- round(rnorm(n=10,
                 mean=10))

#### Store Into Tibble ####
tib <- tibble(
  character,
  x,
  y
)

Issue

Now if I create this plot:

#### Plot ####
tib.plot <- tib %>% 
  ggplot(aes(x,y,
             label=character)) 
  geom_point() 
  geom_label() 
  geom_smooth(se=F) 
  labs(x="Some Variable",
       y="Some Other Variable",
       title = "Simulated Plot") 
  theme_classic()
tib.plot

It looks like this in R's plot window:

enter image description here

However, when I try to save the plot with the Export function, it removes the Chinese characters due to some decoding/encoding issue:

enter image description here

How do I fix this?

CodePudding user response:

If you don't mind your texts will be vectorized you can use pdf() and showtext:

library(showtext)

pdf('plot.pdf', width=8, height=5)
showtext_begin()
tib.plot
dev.off()

PDF with chinese characters

  • Related