2016-07-28 62 views
0

我想在我的.Rnw beamer演示文稿中使用自定義字體。再現誤差的最小工作的例子是:用sweave和beamer自定義ggplot字體

\documentclass{beamer} 
\begin{document} 
\begin{frame}[plain] 
<<echo=FALSE>>= 
library(ggplot2) 
library(extrafont) 
@ 
\begin{figure} 
\centering 
<<label = test, fig=TRUE, include=FALSE, echo=FALSE, message=FALSE>>= 
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point() + 
    ggtitle("Fuel Efficiency of 32 Cars") + 
    xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") + 
    theme_bw() + 
    theme(text=element_text(family="Garamond", size=14)) 
@ 
\includegraphics[width=\textwidth]{test} 
\end{figure} 
\end{frame} 
\end{document} 

和錯誤消息是這樣的:

Writing to file test.tex 
Processing code chunks with options ... 
1 : keep.source term verbatim (test.Rnw:6) 
Registering fonts with R 
2 : keep.source term verbatim pdf (label = test, test.Rnw:12) 
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 

但是,當我在控制檯中使用ggplot命令,一切都看起來不錯。

任何幫助將不勝感激。

+0

的唯一方法我可以重現你的錯誤,使用.Rnw文件,是,如果我嘗試使用不是已經安裝的字體。在控制檯中,如果我嘗試使用卸載的字體,R將恢復爲默認字體併發出警告。 –

+0

這很奇怪。如果我查看'fonts()',我清楚地看到字體已經安裝好了,我想可以通過'extrafont'訪問。 – Pascal

回答

0

好吧,我的問題確實沒有安裝字體。但是因爲我只使用了Garamond作爲例子,並且真的想要使用OTF字體Fira Sans,所以我使用了showtext包。

下面是工作代碼:

\documentclass{beamer} 
\begin{document} 
\begin{frame}[plain] 
<<test, fig=TRUE, echo=FALSE, width=6, height=4>>= 
library(ggplot2) 
library(showtext) 
font.add.google("Fira Sans", "fira") 
showtext.auto() 
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() + 
    ggtitle("Fuel Efficiency of 32 Cars") + 
    xlab("Weight (x1000 lb)") + ylab("Miles per Gallon") + 
    theme_bw() + 
    theme(text = element_text(family = "fira")) 
@ 
\end{frame} 
\end{document}