gpt4 book ai didi

r - Extrafont和ggsave:字符最终在另一个字符之上

转载 作者:行者123 更新时间:2023-12-03 07:29:13 26 4
gpt4 key购买 nike

我目前正在尝试在所有情节中使用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

session 信息

我目前的 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设备
  • 将cairo_pdf指定为设备(这可能会导致输出的次要问题或某些绘图呈现稍有不同)。 PDF很好,这是否可以指出默认PDF设备中的错误?
  • 使用PNG /位图输出(我想使用打印中的图,所以首选 vector 输出)
  • 依靠R使用ArialMT作为默认值。我不确定ArialMT是否与(Latex)文档中使用的字体完全相同,因此我想明确指定字体。
  • 最佳答案

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

    例如

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

    arial_plot

    dev.off()

    关于r - Extrafont和ggsave:字符最终在另一个字符之上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39638529/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com