knitr、cairo和ggplot2:在PostScript字体数据库中找不到字体系列“Source Code Pro”

3
尝试解决我所遇到的otf字体问题(请参见如何在ggplot2图形中使用otf字体?),我发现可以在ggplot2中使用这些字体。
在Rmd文件中使用knitr是可以的:
---
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文件)时,出现了错误:

\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,所以我找到了一个相关的问题,请查看(Yihui的答案):https://dev59.com/Em7Xa4cB1Zd3GeqPuNTS - tonytonov
也许0应该是O? - IRTFM
@tonytonov 首先我想知道为什么我总是得到这一行。但在我的试错过程中,我切换到了Sweave。但将其重置为knitr后,我仍然会收到错误消息:从第15-22行退出(test.Rnw) grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x$y, : invalid font type Calls: knit ... drawDetails -> drawDetails.text -> grid.Call.graphics 此外:有50个或更多警告(使用warnings()查看前50个)执行已停止 - JerryWho
@tonytonov 我忘了提到上面的错误是没有\SweaveOpts行的。 - JerryWho
1个回答

0
我找到了以下的解决方案:
我使用了
\usepackage{tikz}

并且

<<plot, dev='tikz'>>

在我的.Rnw文件中,这样做可以让我的图表使用LaTeX项目的字体。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接