0

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?

Shawn Hemelstrand
  • 2,676
  • 4
  • 17
  • 30
  • Can you find these caracters code in unicode and paste them in your graph code before exporting? For example, you can try to see if it works when you `paste("\u62FF")`. (unfortunately I don't know how to find the characters you wrote) – Yacine Hajji Nov 16 '22 at 09:05
  • Maybe this could help https://stackoverflow.com/questions/1366068/whats-the-complete-range-for-chinese-characters-in-unicode – Yacine Hajji Nov 16 '22 at 09:06
  • Well, theoretically that would help. But given the example I'm thinking of has more than 100 characters, this would take a long time. I'm looking for a simpler solution if possible. – Shawn Hemelstrand Nov 16 '22 at 09:07

1 Answers1

2

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

Robert Hacken
  • 3,878
  • 1
  • 13
  • 15