【问题标题】:Unicode character not shown in pdf file after embeding fonts with extrafonts package嵌入带有额外字体包的字体后,Unicode 字符未显示在 pdf 文件中
【发布时间】:2021-01-29 07:44:20
【问题描述】:

我正在尝试将 ≤ 符号嵌入到我的 pdf 绘图的字体中。在 RStudio (p1) 中查看时,图中会显示 ≤ 符号。但是,当我保存绘图并嵌入字体时,≤ 符号将转换为 = 符号。

使用 extrafont 包我想用 CM Roman 字体保存我的情节。我尝试了将设备设置为ggsave() 中的cairo_pdf 的替代方法。这会将 ≤ 符号嵌入到 pdf 中,但字体不再是 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...

【问题讨论】:

    标签: r ggplot2 extrafont


    【解决方案1】:

    ggsave losing unicode characters from ggplot+gridExtra

    这篇文章中有几个解决方案。我认为它必须与编码有关,但我不是 100% 确定。

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 2010-12-08
      • 2022-01-04
      • 1970-01-01
      • 2015-10-02
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      相关资源
      最近更新 更多