Extrafont and ggsave: Characters end up on top of another
Asked Answered
G

1

31

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.

Minimal working example

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")

This results in the following (correct) PNG images:

Correct arial plot Correct Jokerman plot

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

Jumbled plot

Warning output

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

Session info

My current 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  

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

Workarounds

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. Specify cairo_pdf as a device (This can cause secondary issues with output or some plots to render slightly different). The PDF is fine, so could this point to a bug in the default PDF device?
  2. Use PNG/bitmap output (I want to use the plots in print, so vector output is preferred)
  3. Rely on R using ArialMT as default. I'm not sure if ArialMT is exactly the same font as used in my (Latex) document, so I would like to specify to font explicitly.
Gerkman answered 22/9, 2016 at 12:2 Comment(8)
I'm having the same problem using knitr, ggplot2, and extrafont. For now I've reverted to default fonts (#3 Workaround above).Acetophenetidin
A bit out of topic, but have you try output reports using offifcer package?Bikaner
Same here on OS X, png renders fine but pdf does not, which indeed suggests a bug in the default PDF device. The issue on extrafont is still open. This is up to you, but I'd say playing with the dpi and size parameters of ggsave using the png device will suffice if you embed the plots within a Latex documentPremises
What do you get if you run pdftools::pdf_font("arial_plot.pdf"). Is arial actually embedded?Amaty
before embedding: > pdftools::pdf_fonts("arial_plot.pdf") PDF error: Couldn't find a font for 'ZapfDingbats', subst is 'Helvetica' # A tibble: 2 x 4 name type embedded file <chr> <chr> <lgl> <chr> 1 ZapfDingbats type1 FALSE "C:\\WINDOWS\\Fonts\\arial.ttf" 2 ArialMT type1 FALSE "C:\\WINDOWS\\Fonts\\arial.ttf" Swampland
After embedding: > pdftools::pdf_fonts("arial_plot.pdf") PDF error: Couldn't find a font for 'ZapfDingbats', subst is 'Helvetica' # A tibble: 2 x 4 name type embedded file <chr> <chr> <lgl> <chr> 1 WCBINH+ArialMT truetype TRUE "" 2 ZapfDingbats type1 FALSE "C:\\WINDOWS\\Fonts\\arial.ttf"Swampland
I don't have the same problem (different OS), but it might be worth giving a try to package showtext which works fine for me.Stoat
Weird; do you get the same bug using this code (with & without ggsave)? gist.github.com/grigory93/…Lipp
H
1

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.

e.g

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

arial_plot  

dev.off()
Halfcocked answered 4/5, 2020 at 4:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.