1

I have a report were I output some ggplot-charts with device = svg in rmarkdown. Output-format is html. Unfortunently I get lots of warnings like below. I don't get any warnings if I specify device = png

## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font width unknown for character 0x53

Anybody knows why png works (or atleast doesn't give any warnings) but not svg? Session info and non-reproducible code below. I use extrafont which I guess may have something to do with it.

Sessioninfo:

R version 3.3.1 (2016-06-21)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

locale:
[1] LC_COLLATE=Swedish_Finland.1252  LC_CTYPE=Swedish_Finland.1252
    LC_MONETARY=Swedish_Finland.1252 LC_NUMERIC=C                    
[5] LC_TIME=Swedish_Finland.1252    

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

other attached packages:
[1] extrafont_0.17   cairoDevice_2.23 Cairo_1.5-9      RODBC_1.3-13    

loaded via a namespace (and not attached):
[1] magrittr_1.5    htmltools_0.3.5 tools_3.3.1     yaml_2.1.13  
    Rcpp_0.12.5     stringi_1.1.1   rmarkdown_1.0   extrafontdb_1.0
[9] knitr_1.13      stringr_1.0.0   digest_0.6.9    Rttf2pt1_1.3.4
    evaluate_0.9   

Code-chunk:

```{r plot2, echo = FALSE, fig.show='hold', out.width='50%', dev=c('svg')}

ggplot-code example:

ggplot(dt.bar2_1, 
   aes(x=factor(Mon), y=nsk_rel, fill =Class)) +
   geom_bar(stat="identity", position = "dodge")+
   scale_fill_manual(values = c("#6baed6","#3182bd"), name = "Type") +
   scale_y_continuous(labels = percent, 
                      breaks = c(0, 0.02, 0.04, 0.06, 0.08, 0.10), 
                      limits = c(0, 0.10))+
   theme(axis.text.x = element_text(size=11),
         axis.text.y = element_text(size=11))+
   xlab("Month") + ylab("Freq")+
   ggtitle(Title.2_1) +
   theme(plot.title = element_text(family = "Trebuchet MS", 
                                   color="#666666", 
                                   face="bold", 
                                   size=16,
                                   hjust=0))

When calling fonttable():

Screenshot

ErrantBard
  • 1,421
  • 1
  • 21
  • 40

3 Answers3

1

The problem is, that the svg device does not know the font Trebucht MS. Take a look at the package extrafont. Install it and import all system fonts with

install.packages("extrafont")
library(extrafont)
font_import()

This will take some minutes. Afterwards put these lines in your RMarkdown document:

```{r}
library(extrafont)
loadfonts(device = "pdf", quiet = T)
```

This should do the trick as long as Trebuchet MS is available on your system.

Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
  • Thanks @Martin, I actually have the extrafont package loaded and fonts imported. The last line, loadfonts I haven't used though. So tried it but it didn't seem to help. I'm starting to think I should let all this slide even if it irks me that png works but not svg! – ErrantBard Aug 19 '16 at 05:56
  • And when I think about it - the titles for my charts sure look like they have trebuchet as a font, despite warning. If I don't run the theme plot-title part I get another font. – ErrantBard Aug 19 '16 at 06:19
  • @ErrantBard Interesting. It works for me though. Are you sure Trebuchet MS is registered in the extrafontdb? – Martin Schmelzer Aug 19 '16 at 07:37
  • Interesting indeed - something is weird and I don't doubt it is connected to Extrafonts as you say :) See image above, from when I call fonttable. – ErrantBard Aug 19 '16 at 08:09
  • @Martin Schmelzer: I ran into a similar problem, but none of the solutions proposed here and elsewhere on SO helped. Here my questionhttps://stackoverflow.com/questions/62067139/warning-in-grid-callc-textbounds-as-graphicsannotxlabel-xx-xy-font-w Any help is greatly appreciated! – mavericks May 28 '20 at 14:46
1

I'm not sure whether you still need any help till. However I've encountered this problem today and I finally find an simple approach. I post it here in case someone need in the future.

Here's the code:

windowsFonts(myFont = windowsFont("TT Times New Rome"))

line_plot = ggplot(data = total_plot_data, aes(x = hour, y = electricity, group = type, colour = type, linetype = type)) + 
  geom_line(size = 0.75) + 
  theme(
    text = element_text(family = "myFont", size = 15),
    legend.text = element_text(size = 10),
  );

Simply add a line as windowsFonts(myFont = windowsFont("TT Times New Rome")) and use element_text(family = "myFont", size = 15) solve this problem on my computer.

Xinz
  • 64
  • 3
0

Also late response here. For whatever reason, pdf and svg seem to have some trouble with this font even if the font is correctly loaded. If it is an option, you could set dev='cairo_pdf' for vectorised graphics. This should work also for normal R scripts.

Jinglestar
  • 376
  • 1
  • 10