2

I wanted to create a plot with ggplot and save it with ggsave(). As a device I used cairo_pdf, because I want to embed the font. The problem is that if the plot contains an umlaut I can create the plot fine in RStudio, but if I want to save it I get the following error:

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : Metric information not available for this family/device

Here is a short example demonstrating the problem. The problem seems to be caused by an umlaut and using label_parsed.

library(ggplot2)

df <- data.frame(x = rnorm(10), 
                 y = rnorm(10), 
                 group = factor(rbinom(100, 1, .5), labels = c(expression("A^b"), expression("Ü"))))

ggplot(df, aes(x = x, y = y)) + geom_point() + facet_grid(. ~ group, labeller = label_parsed)
ggsave("test.pdf", device = cairo_pdf)

Here is my session Info:

R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

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] ggplot2_3.3.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2       withr_2.1.2      assertthat_0.2.1 crayon_1.3.4     dplyr_0.8.3      grid_3.6.3      
 [7] R6_2.4.0         gtable_0.3.0     magrittr_1.5     scales_1.0.0     pillar_1.4.2     rlang_0.4.5     
[13] lazyeval_0.2.2   rstudioapi_0.10  tools_3.6.3      glue_1.3.1       purrr_0.3.3      munsell_0.5.0   
[19] compiler_3.6.3   pkgconfig_2.0.3  colorspace_1.4-1 tidyselect_0.2.5 tibble_2.1.3
Gilean0709
  • 1,098
  • 6
  • 17
  • Does this answer your question? [R crashes upon exporting ggplot objects with unicode in axis expression](https://stackoverflow.com/questions/42722446/r-crashes-upon-exporting-ggplot-objects-with-unicode-in-axis-expression) – caldwellst Apr 28 '20 at 08:49
  • @caldwellst I checked that answer. I cannot change the device to `pdf` because of other requirements and the `sprintf()`-solution did not work instead of the expressions. – Gilean0709 Apr 28 '20 at 09:09
  • I cannot reproduce this. Are you using a very special font? Umlauts are not very uncommon characters - actually, have you tried just printing them *without* expression? Works fine for me (i.e., `labels = c(expression("A^b"), "Ü")`) – tjebo Apr 28 '20 at 09:16
  • @Tjebo The problem is that one of the facet label is an expression and therefore I need to use label_parsed. It actually does not matter if I put in an expression or not in this example. The error occurs as long as I use `label_parsed`. – Gilean0709 Apr 28 '20 at 09:19
  • Yes, still works for me. Maybe restart R etc. Empty the session. Check versions (and update). With your exact code running fine on `R version 3.6.3 (2020-02-29) / ggplot2 3.3.0 Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS Mojave 10.14.6. ` – tjebo Apr 28 '20 at 09:19
  • @Tjebo I tried your solution, but I still get the same error when I want to save the plot. – Gilean0709 Apr 28 '20 at 09:26
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/212680/discussion-between-gilean0709-and-tjebo). – Gilean0709 Apr 28 '20 at 09:32
  • I get the feeling that this is an encoding problem - possibly related https://stackoverflow.com/questions/16347731/how-to-change-the-locale-of-r – tjebo Apr 28 '20 at 09:45

0 Answers0