【问题标题】:Embedding fonts in ggplot2 charts in rmarkdown documents在 rmarkdown 文档的 ggplot2 图表中嵌入字体
【发布时间】:2016-07-26 23:22:30
【问题描述】:

我想在 ggplot2 图表中使用非标准字体,然后将其嵌入到 rmarkdown 文档中,然后将其编入 PDF。我目前的工作流程是在图表中指定字体,然后编织,然后在创建的 PDF 上运行 extrafonts::embed_fonts。我的问题是:我可以直接在 rmarkdown 文档中指定字体应该嵌入到输出的 PDF 中吗?

小例子:

---
title: "Untitled"
output: beamer_presentation
---

```{r}
library(extrafont)
library(ggplot2)
loadfonts()
qplot(iris$Sepal.Length) + theme_light(base_family = "CM Roman")
```

knitr::knit2pdf("test.rmd")
embed_fonts("test.pdf")

【问题讨论】:

    标签: r fonts ggplot2 knitr r-markdown


    【解决方案1】:

    如果您将图形设备设置为“cairo_pdf”,则会嵌入字体。您可以使用 knitr::opts_chunk$set 对单个块或整个文档执行此操作

    我在下面使用了一种非常明显不同的字体,因此很明显字体确实是被设置的。

    这个包叫做“extrafont”而不是“extrafonts”

    ---
    title: "Untitled"
    output: beamer_presentation
    ---
    
    ```{r, echo=FALSE, message = FALSE}
    knitr::opts_chunk$set(warning=FALSE, message=FALSE, echo = FALSE, dev = "cairo_pdf")
    ```
    
    ```{r}
    library(extrafont)
    library(ggplot2)
    loadfonts()
    ```
    
    
    ##
    
    ```{r, fig.width = 5}
    qplot(iris$Sepal.Length) + theme_light(base_family = "Vladimir Script")
    ```
    

    【讨论】:

    猜你喜欢
    • 2015-05-04
    • 2018-11-05
    • 1970-01-01
    • 2018-02-13
    • 2017-01-14
    • 2018-05-25
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    相关资源
    最近更新 更多