Extrafont和ggsave:字符最终出现在另一个字符之上 [英] Extrafont and ggsave: Characters end up on top of another

查看:109
本文介绍了Extrafont和ggsave:字符最终出现在另一个字符之上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently trying to use Arial.ttf in all my plots. I have found several manuals about fonts online, I'm however running into issues when I try to save the pot via ggsave and the default device.

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

I'm running the following minimal working example to reproduce this bug:

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图像:

This results in the following (correct) PNG images:

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

The Arial PDF, however, has all characters stacked on top of each other (the Jokerman PDF renders correct):

此脚本生成以下警告消息.

The following warning messages are generated by this script.

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
  • Attempted solutions

    • I've tried several ways of renaming the generated AMF files to no avail
    • I've thought that maybe the encoding of my source file might matter, so I changed it to UTF-8 (no change)
    • Resetting extrafontdb multiple times
    • Reinstalling ghostscript multiple times
    • 我目前有两种解决方法,但是我更喜欢使用R或ggplot中配置的本机" PDF设备,以便于移植.

      I have currently two workarounds, I would, however, prefer to use the "native" PDF device as configured in R or ggplot for portability sake

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

      推荐答案

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

      Instead of using ggsave(), try exporting it using the code below into your working directory run the three(3) lines together, It should be able to solve the problem for you. Comment below if you have further questions.

      例如

      png("arial_plot.png", width = 8, height = 7, units = 'in', res = 300, compression ='lzw')     
      
      arial_plot  
      
      dev.off()
      

      这篇关于Extrafont和ggsave:字符最终出现在另一个字符之上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆