当另存为 PDF 时,如何使 R 绘图使用拉丁现代字体系列? [英] How can I make an R plot use the Latin Modern font family when saved as a PDF?

查看:22
本文介绍了当另存为 PDF 时,如何使 R 绘图使用拉丁现代字体系列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我输入

par(family='CM Roman')

然后我可以得到一个用 Computer Modern 字体显示的图.但是,当我尝试将其另存为 PDF 时,它会显示为空白文档,这与我删除该行不同.

我怎样才能让它正确地保存为 PDF?

另外,我怎样才能使用拉丁现代而不是计算机现代?

解决方案

这就是我在 Windows 中的做法:

  1. 安装 extrafont 包.
  2. 安装拉丁现代字体,例如来自

    导出的 pdf:

    If I type

    par(family='CM Roman')
    

    then I can get a plot to display with the Computer Modern font. However, when I try to save it as a PDF, it comes up as a blank document, unlike if I take out that line.

    How can I get it to save correctly as a PDF?

    Also, how can I use Latin Modern instead of Computer Modern?

    解决方案

    This is how I did it in Windows:

    1. Install the extrafont package.
    2. Install Latin Modern fonts, e.g. from http://www.fontsquirrel.com/fonts/latin-modern-roman. Watch out, you need to install the TTF version of the font, font_import() can't handle OTF.
    3. Import the fonts using font_import().
    4. Load the fonts using loadfonts(device = "win"). Use the device = "Win" parameter to make the preview in R Studio work.
    5. Set the font family graphics parameter using par(family = "LM Roman 10").
    6. Plotting in R Studio now works and so does pdf export (see the pictures below).

    This is the full code you need to use:

    # Run once
    install.packages("extrafont")
    library(extrafont)
    # Install **TTF** Latin Modern Roman fonts from www.fontsquirrel.com/fonts/latin-modern-roman
    # Import the newly installed LModern fonts, change the pattern according to the 
    # filename of the lmodern ttf files in your fonts folder
    font_import(pattern = "lmroman*")
    
    
    # Run each time
    library(extrafont)
    loadfonts(device = "win")
    par(family = "LM Roman 10")
    x <- seq(1, 10, 1)
    y <- seq(1, 10, 1)
    plot(y ~ x, main="This plot uses LaTeX font!", ylab = expression(alpha))
    

    R Studio preview:

    Exported pdf:

    这篇关于当另存为 PDF 时,如何使 R 绘图使用拉丁现代字体系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆