R crashes upon exporting ggplot objects with unicode in axis expression

孤人 提交于 2020-08-07 06:47:05

问题


This code

    data = data.frame(A=c(1:100), B=runif(100))
    library(ggplot2)
    ggplot(data, aes(A, B)) +
      geom_point() +
      ylab(expression(paste(delta^{13}~"C"~"VDPB (\u2030/)"))) +
      xlab("Time (Days)")
    ggsave("test.pdf", width=4, height=4, dpi=300, device=cairo_pdf)

crashes my R-Session with

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

although the expression is drawn correctly on the R-Studio graphical device.

After reviewing google, this problem is known, but i havent found a solution for it yet. Is there any way to circumvent this problem?

/edit: Session Info

    R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

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] Cairo_1.5-9        rvg_0.1.2          scales_0.4.1       gridExtra_2.2.1    RColorBrewer_1.1-2
[6] reshape_0.8.6      ggplot2_2.2.1     

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.9       digest_0.6.12     assertthat_0.1    R.methodsS3_1.7.1 grid_3.3.3       
 [6] plyr_1.8.4        gtable_0.2.0      gdtools_0.1.3     lazyeval_0.2.0    R.oo_1.21.0      
[11] R.utils_2.5.0     xml2_1.1.1        labeling_0.3      tools_3.3.3       munsell_0.4.3    
[16] colorspace_1.3-2  tibble_1.2      

回答1:


Old (okay) answer: Change ggsave("test.pdf", width=4, height=4, dpi=300, device=cairo_pdf) to ggsave("test.pdf", width=4, height=4, dpi=300, device="pdf"). I just changed the device.

Edit: You can continue to use cairo_pdf as a device if you change your ylab creation to ylab(sprintf(paste(delta^{13}~"C"~"VDPB (\u2030/)"))) changing expression to sprintf. However, your y-label won't totally show up. it seems you're using some character that cairo_pdf doesn't like. So this could further solve the root of the problem.



来源:https://stackoverflow.com/questions/42722446/r-crashes-upon-exporting-ggplot-objects-with-unicode-in-axis-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!