【问题标题】:knitr, cairo and ggplot2: font family 'Source Code Pro' not found in PostScript fontdatabaseknitr、cairo 和 ggplot2:在 PostScript 字体数据库中找不到字体系列“Source Code Pro”
【发布时间】:2015-06-30 11:06:20
【问题描述】:

尝试解决我拥有的 otf-fonts 的问题(请参阅How to use otf-font in ggplot2 graphics?)我发现我可以将这些字体与 ggplot2 一起使用。

将 knitr 与 Rmd 文件一起使用是可以的:

---
title: "FontTest"
author: "me"
date: "22. April 2015"
output: html_document
---

```{r, echo=FALSE}
library(ggplot2)

ggplot(mtcars, aes(x=wt, y=mpg, color=factor(gear))) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=16, family="Arial"))
```

但是当我想使用 LaTeX (Rnw-files) 时出现错误:

\documentclass{article}

\begin{document}
\SweaveOpts{concordance=TRUE}

<<>>=
Sys.setenv(LANG = "en")
library(ggplot2, quietly=TRUE)
@



<<>>=
ggplot(mtcars, aes(x=wt, y=mpg, color=factor(gear))) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=16, family="Arial"))
@

\end{document}

这是错误:

Writing to file test.tex
Processing code chunks with options ...
 1 : echo keep.source term verbatim (test.Rnw:6)
 2 : echo keep.source term verbatim (test.Rnw:13)
Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
  invalid font type
Calls: <Anonymous> ... drawDetails -> drawDetails.text -> grid.Call.graphics
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Execution halted

我正在使用 Mac OS X Mavericks。

更新

我找到了使用 Cairo 的解决方法:

\documentclass{article}

\begin{document}



<<>>=
  Sys.setenv(LANG = "en")
library(ggplot2, quietly=TRUE)
library(Cairo)
@



<<dev='cairo_pdf'>>=

  ggplot(mtcars, aes(x=wt, y=mpg, color=factor(gear))) + geom_point() +
  ggtitle("Fuel Efficiency of 32 Cars") +
  xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") +
  theme(text=element_text(size=16, family="Source Code Pro"))

@

\end{document}

但现在我收到很多警告:

## Warning in grid.Call(LtextBounds, as.graphicsAnnot(x$label), x$x,x$y, :  font family 'Source Code Pro' not found in PostScript fontdatabase

我想避免隐藏这些警告。那么我该怎么做才能摆脱这些警告呢?

【问题讨论】:

  • 我想知道为什么你有\SweaveOpts,所以我发现了一些相关的问题,检查一下(一辉的回答):stackoverflow.com/questions/15040064/…
  • 也许 0 应该是 O?
  • @tonytonov 首先我想知道为什么我总是把这条线拿回来。但在我反复试验的过程中,我切换到了 Sweave。但是将其重置为 knitr 我仍然收到错误:Quitting from lines 15-22 (test.Rnw) Error in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, :无效字体类型调用:knit ... drawDetails -> drawDetails.text -> grid.Call.graphics 另外:有 50 个或更多警告(使用 warnings() 查看前 50 个)执行停止
  • @tonytonov 我忘了提到上面的错误没有 \SweaveOpts 行。

标签: r fonts ggplot2 knitr cairo


【解决方案1】:

我找到了以下解决方案:

我用

\usepackage{tikz}

<<plot, dev='tikz'>>

在我的 .Rnw 文件中。这样做我可以在我的绘图中获得我的 LaTeX 项目的字体。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 2021-02-04
    • 2015-11-04
    • 2013-09-05
    • 2017-02-01
    • 2018-04-18
    • 1970-01-01
    • 2018-10-24
    相关资源
    最近更新 更多