简体   繁体   English

使用 extrafonts 包嵌入字体后,Unicode 字符未显示在 pdf 文件中

[英]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.我正在尝试将 ≤ 符号嵌入到我的 pdf 绘图的字体中。 The ≤ sign is shown in the plot when viewed within RStudio ( p1 ).在 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.使用 extrafont 包,我想用 CM Roman 字体保存我的绘图。 I have tried alternative methods with the device set as cairo_pdf in ggsave() .我已经尝试了在ggsave()中将设备设置为cairo_pdf替代方法。 This embeds the ≤ sign into the pdf, but the font is no longer CM Roman.这将 ≤ 符号嵌入到 pdf 中,但字体不再是 CM Roman。

What needs to be done so that the ≤ sign remains in the plot and the font is CM Roman?需要做什么才能使 ≤ 符号保留在绘图中并且字体为 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, "4\u2264")))


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...

ggsave losing unicode characters from ggplot+gridExtra ggsave 从 ggplot+gridExtra 中丢失 unicode 字符

There are several solutions in this post.这篇文章中有几种解决方案。 I think it has to do something with encoding, but I am not 100% sure.我认为它必须与编码有关,但我不是 100% 确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM