尝试使用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文件)时,我收到一个错误:
\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。
更新
我找到了一个使用开罗的工作:
\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
我想避免压制这些警告。那么我该怎样做才能摆脱这些警告?
答案 0 :(得分:0)
我找到了以下解决方案:
我用
\usepackage{tikz}
和
<<plot, dev='tikz'>>
在我的.Rnw文件中。这样做我在我的图中得到了我的LaTeX项目的字体。