Unicode Characters in ggplot2 PDF Output

前端 未结 3 830
迷失自我
迷失自我 2020-11-28 10:04

How can I use Unicode characters for labels, titles and similar things in a PDF plot created with ggplot2?

Consider the following example:

library(gg         


        
3条回答
  •  渐次进展
    2020-11-28 10:41

    As of 2020 and R version 4.0.3, cairo_pdf() is not your friend anymore on Mac OS X, at least as far as Cyrillic is concerned — See Fail Gallery below.

    TL;DR

    If you must have Cyrillic, just go back to good ole png driver. (And kiss your antialiased diagrams goodbye.)

    R -e 'png(filename = "ftw.png"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
    open ftw.png
    

    Or if you use Rmarkdown with knitr:

    R -e 'rmarkdown::render("foo.Rmd", "pdf_document", output_file="foo.pdf", runtime = "static", output_options = list(dev = "png"))'
    

    The Fail Gallery

    The “modern” approach with Cairo fails in v4.0.3 as demonstrated below. Note that this is not (or not only) a font embedding or rendering problem, since selecting and pasting text out of the generated PDFs also produces garbled output.

    Prep steps:

    1. install the latest R (version 4.0.3 or higher, with all capabilities() showing TRUE)
    2. R -e 'install.packages(c("Cairo", "ggplot2"), repos="https://cloud.r-project.org")'

    Vanilla config

    R -e 'library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); ggsave("fail1.pdf")'
    open fail1.pdf
    

    Using cairo_pdf() alone

    R -e 'cairo_pdf("fail2.pdf"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
    open fail2.pdf
    

    Using cairo_pdf() with a custom (supposedly Unicode-compliant) font

    R -e 'cairo_pdf("fail3.pdf", family = "Arial Unicode MS"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
    open fail3.pdf
    

    Another attempt with Comic Sans for good measure:

    R -e 'cairo_pdf("fail3bis.pdf", family = "Comic Sans MS"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
    open fail3bis.pdf
    

    A few more...

    With the older "Dark and Stormy Night" version (3.6.2):

    /Library/Frameworks/R.framework/Versions/3.6/Resources/bin/R -e 'cairo_pdf("fail4.pdf", family = "Arial Unicode MS"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
    open fail4.pdf
    

    And with DejaVu Sans as suggested by @drammock:

    R -e 'cairo_pdf("fail5.pdf", family = "DejaVu Sans"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
    open fail5.pdf
    

    DejaVu Sans on older R:

    /Library/Frameworks/R.framework/Versions/3.6/Resources/bin/R -e 'cairo_pdf("fail5bis.pdf", family = "DejaVu Sans"); library(ggplot2); qplot(Sepal.Length, Petal.Length, data=iris, main="Aʙᴄᴅᴇғɢʜɪᴊᴋʟᴍɴᴏᴘǫʀsᴛᴜᴠᴡxʏᴢ"); dev.off()'
    open fail5bis.pdf
    

提交回复
热议问题