Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

r - Unicode character not shown in pdf file after embeding fonts with extrafonts package

I am trying to embed the ≤ sign into the fonts of my pdf plot. The ≤ sign is shown in the plot when viewed within RStudio (p1). However, when I save the plot and embed the fonts the ≤ sign is converted to an = sign.

Using the extrafont package I want to save my plot with the CM Roman font. I have tried alternative methods with the device set as cairo_pdf in ggsave(). This embeds the ≤ sign into the pdf, but the font is no longer CM Roman.

What needs to be done so that the ≤ sign remains in the plot and the font is CM Roman?

library(ggplot2)
library(extrafont)

font_import()
font_install("fontcm")
loadfonts()

df <- data.frame(foo = c(2, 4, 8 , 16),
                 bar = factor(c(1:3, "4u2264")))


p1 <- ggplot(df, aes(x = bar, y = foo)) + 
  geom_bar(stat="identity") +
  labs(title = "p1: Equality sign shows in RStudio plot") +
  theme(text = element_text(family = "CM Roman", size = 25))

p2 <- ggplot(df, aes(x = bar, y = foo)) + 
  geom_bar(stat="identity") +
  labs(title = "p2: Equality sign not shown in .pdf file") +
  theme(text = element_text(family = "CM Roman", size = 25)) 

print(p1)
[![p1 plot][1]][1] # there should be an image of p1 here...

# Sys.setenv(R_GSCMD = "C:/Program Files/gs/gs9.53.3/bin/gswin64c.exe") #~ run once at start of each R session  
ggsave("p2.pdf", p2, width = 15, height = 10, units = "in")
embed_fonts("p2.pdf", outfile="p2.pdf")
[![p2 plot][1]][1] # there should be an image of p2 here...
question from:https://stackoverflow.com/questions/65950508/unicode-character-not-shown-in-pdf-file-after-embeding-fonts-with-extrafonts-pac

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

ggsave losing unicode characters from ggplot+gridExtra

There are several solutions in this post. I think it has to do something with encoding, but I am not 100% sure.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...