Extrafont和ggsave:字符最终出现在另一个字符之上

问题描述 投票:28回答:1

我目前正在所有情节中尝试使用Arial.ttf。我在线上找到了几本有关字体的手册,但是当我尝试通过ggsave和默认设备保存电位器时遇到了问题。

最小工作示例

我正在运行以下最小的工作示例来重现此错误:

Sys.setenv(R_GSCMD="D:/gs/bin/gswin64c.exe")

library(ggplot2)

require(extrafont)
font_import(prompt = F, pattern = "arial.ttf")
font_import(prompt = F, pattern = "JOKERMAN.TTF")
loadfonts(device="win", quiet = T)
loadfonts(device="pdf", quiet = T)

plot <- ggplot(mtcars, aes(wt, mpg, colour = hp)) + geom_point()
jokerman_plot <- plot + theme_bw(base_family = "Jokerman")
arial_plot <- plot + theme_bw(base_family = "Arial")

ggsave("arial_plot.png", arial_plot)
ggsave("arial_plot.pdf", arial_plot)
embed_fonts("arial_plot.pdf")

ggsave("jokerman_plot.png", jokerman_plot)
ggsave("jokerman_plot.pdf", jokerman_plot)
embed_fonts("jokerman_plot.pdf")

这将产生以下(正确的)PNG图像:

[Correct arial plot Correct Jokerman plot

但是,Arial PDF的所有字符都堆叠在一起(Jokerman PDF呈现正确的字符:]

Jumbled plot

警告输出

以下警告消息由此脚本生成。

Warning messages:
1: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x68
2: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x70
3: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font metrics unknown for character 0x4d
4: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x68
5: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x70
6: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x4e
7: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x41
8: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font metrics unknown for character 0x4d
9: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x31
10: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
11: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
12: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x31
13: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x35
14: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
15: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x32
16: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
17: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
18: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x32
19: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x35
20: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
21: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x33
22: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
23: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
24: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x4e
25: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x41
26: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x4e
27: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x41
28: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x31
29: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
30: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
31: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x31
32: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x35
33: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
34: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x32
35: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
36: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
37: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x32
38: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x35
39: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
40: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x33
41: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
42: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x30
43: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x4e
44: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x41
45: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x6d
46: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x70
47: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x67
48: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font metrics unknown for character 0x4d
49: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x6d
50: In grid.Call(L_textBounds, as.graphicsAnnot(x$label),  ... :
  font width unknown for character 0x70

会话信息

我当前的sessionInfo()

R version 3.3.0 (2016-05-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] extrafont_0.17      ggplot2_2.1.0       RevoUtilsMath_3.2.5

loaded via a namespace (and not attached):
 [1] labeling_0.3     colorspace_1.2-6 scales_0.4.0     assertthat_0.1   plyr_1.8.3       tools_3.3.0     
 [7] gtable_0.2.0     ggthemes_3.0.3   Rcpp_0.12.5      extrafontdb_1.0  grid_3.3.0       digest_0.6.9    
[13] Rttf2pt1_1.3.4   munsell_0.4.3  

尝试的解决方案

  • 我已经尝试了几种将生成的AMF文件重命名为无效的方法
  • 我以为也许我的源文件的编码可能很重要,所以我将其更改为UTF-8(不变)
  • 多次重设extrafontdb
  • 多次重新安装ghostscript

解决方法

我目前有两种解决方法,但是,为了可移植性,我更喜欢使用R或ggplot中配置的“本地” PDF设备

  1. 将cairo_pdf指定为设备(这可能会导致输出的次要问题或某些绘图的绘制略有不同)。 PDF很好,那么这是否可以指出默认PDF设备中的错误?
  2. 使用PNG /位图输出(我想使用打印中的图,因此首选矢量输出)
  3. 依靠R使用ArialMT作为默认值。我不确定ArialMT是否与(Latex)文档中使用的字体完全相同,因此我想明确指定字体。
r ggplot2 extrafont ggsave
1个回答
0
投票

而不是使用ggsave,请尝试使用以下代码将其导出到您的工作目录中同时运行三(3)行,它应该能够为您解决问题。如果您还有其他问题,请在下面评论。

例如

png(“ arial_plot.png”,宽度= 8,高度= 7,单位='英寸',分辨率= 300,压缩='lzw')

arial_plot

dev.off()

© www.soinside.com 2019 - 2024. All rights reserved.